Template Matching On Images C# using OpenCV

Image<Bgr, byte> Image1 = new Image<Bgr, byte>(Properties.Resources.Image1); //Your first image
        Image<Bgr, byte> Image2 = new Image<Bgr, byte>(Properties.Resources.Image2); //Your second image

        double Threshold = 0.8; //set it to a decimal value between 0 and 1.00, 1.00 meaning that the images must be identical

        Image<Gray, float> Matches = Image1.MatchTemplate(Image2, TemplateMatchingType.CcoeffNormed);

        for (int y = 0; y < Matches.Data.GetLength(0); y++)
        {
            for (int x = 0; x < Matches.Data.GetLength(1); x++)
            {
                 if (Matches.Data[y, x, 0] >= Threshold) //Check if its a valid match
                 {
                     //Image2 found within Image1
                 }
            }
        }

References
https://stackoverflow.com/questions/39570033/template-matching-on-images-c-sharp

Move and Click Mouse in C#

[DllImport("user32.dll")]
public static extern void mouse_event(MouseEventFlags dwFlags, uint dx, uint dy, uint dwData, MouseEventDataXButtons dwExtraInfo);

[DllImport("User32.Dll")]
public static extern long SetCursorPos(int x, int y);
[Flags]
public enum MouseEventFlags : uint
{
    LEFTDOWN = 0x00000002,
    LEFTUP = 0x00000004,
    MIDDLEDOWN = 0x00000020,
    MIDDLEUP = 0x00000040,
    MOVE = 0x00000001,
    ABSOLUTE = 0x00008000,
    RIGHTDOWN = 0x00000008,
    RIGHTUP = 0x00000010,
    WHEEL = 0x00000800,
    XDOWN = 0x00000080,
    XUP = 0x00000100
}

//Use the values of this enum for the 'dwData' parameter
//to specify an X button when using MouseEventFlags.XDOWN or
//MouseEventFlags.XUP for the dwFlags parameter.
public enum MouseEventDataXButtons : uint
{
    XBUTTON1 = 0x00000001,
    XBUTTON2 = 0x00000002
}
public static void MoveMouse(int x, int y)
{
    PInvoke.SetCursorPos(x, y);
}

public static void ClickMouse(int x, int y)
{
    PInvoke.mouse_event(MouseEventFlags.LEFTDOWN | MouseEventFlags.ABSOLUTE, (uint)x, (uint)y, 0, 0);
    Thread.Sleep(10);
    PInvoke.mouse_event(MouseEventFlags.LEFTUP | MouseEventFlags.ABSOLUTE, (uint)x, (uint)y, 0, 0);
}

References
https://stackoverflow.com/questions/13520705/move-mouse-to-position-and-left-click
http://www.pinvoke.net/default.aspx/user32.mouse_event
https://stackoverflow.com/questions/8050825/how-to-move-mouse-cursor-using-c
https://www.pinvoke.net/default.aspx/user32.setcursorpos

Take Screenshot in C#

First Set Process DPI Aware:

Get Screen Size on C#
https://pupli.net/2020/03/get-screen-size-on-c/

Install-Package System.Drawing.Common -Version 7.0.0

Take screenshot and save to file

using System;
using System.Drawing;
using System.Windows.Forms;

namespace ScreenshotExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get the bounds of the screen
            Rectangle bounds = Screen.PrimaryScreen.Bounds;

            // Create a bitmap object to store the screenshot
            using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
            {
                // Create a graphics object from the bitmap
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    // Capture the screen
                    g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
                }

                // Save the screenshot as a PNG file
                bitmap.Save("screenshot.png");
            }

            Console.WriteLine("Screenshot saved as screenshot.png");
        }
    }
}

Take screenshot and save to memory

using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace ScreenshotToMemory
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get the bounds of the screen
            Rectangle bounds = Screen.PrimaryScreen.Bounds;

            // Create a bitmap object to store the screenshot
            using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
            {
                // Create a graphics object from the bitmap
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    // Capture the screen
                    g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
                }

                // Store the screenshot in memory using MemoryStream
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);

                    // Convert the MemoryStream to byte array
                    byte[] screenshotBytes = memoryStream.ToArray();

                    // Now the screenshot is stored in screenshotBytes, you can use it as needed
                    // For demonstration, let's output the length of the byte array
                    Console.WriteLine($"Screenshot captured. Byte array length: {screenshotBytes.Length}");
                }
            }
        }
    }
}

 

Get Screen Size on C#

The process is running under DPI virtualization. So we should apply the DPI aware manifest option to stop DPI virtualization

[DllImport("user32.dll", SetLastError=true)]
static extern bool SetProcessDPIAware();

Then

public enum SystemMetric
{
    VirtualScreenWidth = 78, // CXVIRTUALSCREEN 0x0000004E 
    VirtualScreenHeight = 79, // CYVIRTUALSCREEN 0x0000004F 
}

[DllImport("user32.dll")]
public static extern int GetSystemMetrics(SystemMetric metric);

public static Size GetVirtualDisplaySize()
{
    var width = GetSystemMetrics(SystemMetric.VirtualScreenWidth);
    var height = GetSystemMetrics(SystemMetric.VirtualScreenHeight);

    return new Size(width, height);
}

References
https://stackoverflow.com/questions/28339257/getsystemmetrics-and-tscreen-returns-wrong-value
https://www.pinvoke.net/default.aspx/user32/SetProcessDPIAware.html
https://stackoverflow.com/questions/1317235/c-get-complete-desktop-size
https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setprocessdpiaware

Write a Siemens S7 plc driver with C# and Sharp7

Connection to the plc

// Create and connect the client
var client = new S7Client();
int result = client.ConnectTo("127.0.0.1", 0, 1);
if(result == 0)
{
    Console.WriteLine("Connected to 127.0.0.1");
}
else
{
    Console.WriteLine(client.ErrorText(result));
}
 
// Disconnect the client
client.Disconnect();

Read from DB

Console.WriteLine("\n---- Read DB 1");
 
byte[] db1Buffer = new byte[18];
result = client.DBRead(1, 0, 18, db1Buffer);
if(result != 0)
{
    Console.WriteLine("Error: " + client.ErrorText(result));
}
int db1dbw2= S7.GetIntAt(db1Buffer, 2);
Console.WriteLine("DB1.DBW2: " + db1dbw2);
 
double db1ddd4 = S7.GetRealAt(db1Buffer, 4);
Console.WriteLine("DB1.DBD4: " + db1ddd4);
 
double db1dbd8 = S7.GetDIntAt(db1Buffer, 8);
Console.WriteLine("DB1.DBD8: " + db1dbd8);
 
double db1dbd12 = S7.GetDWordAt(db1Buffer, 12);
Console.WriteLine("DB1.DBD12: " + db1dbd12);
 
double db1dbw16 = S7.GetWordAt(db1Buffer, 16);
Console.WriteLine("DB1.DBD16: " + db1dbw16);

Write to DB

db1Buffer = new byte[12];
const int START_INDEX = 4;
S7.SetRealAt(db1Buffer, 4 - START_INDEX, (float)54.36);
S7.SetDIntAt(db1Buffer, 8 - START_INDEX, 555666);
S7.SetDWordAt(db1Buffer, 12 - START_INDEX, 123456);
result = client.DBWrite(1, START_INDEX, db1Buffer.Length, db1Buffer);
if (result != 0)
{
    Console.WriteLine("Error: " + client.ErrorText(result));
}

ReadMultiVar

// Read multi vars
 
var s7MultiVar = new S7MultiVar(client);
byte[] db1 = new byte[18];
s7MultiVar.Add(S7Consts.S7AreaDB, S7Consts.S7WLByte, 1, 0, 18, ref db1);
byte[] db3 = new byte[18];
s7MultiVar.Add(S7Consts.S7AreaDB, S7Consts.S7WLByte, 3, 0, 18, ref db3);
result = s7MultiVar.Read();
if (result != 0)
{
    Console.WriteLine("Error on s7MultiVar.Read()");
}
db1dbw2 = S7.GetIntAt(db1, 2);
Console.WriteLine("DB1.DBW2.0 = {0}", db1dbw2);
 
db1ddd4 = S7.GetRealAt(db1, 4);
Console.WriteLine("DB1.DBW4.0 = {0}", db1ddd4);
 
db1dbd8 = S7.GetDIntAt(db1, 8);
Console.WriteLine("DB1.DBW8.0 = {0}", db1dbd8);
 
db3dbw2 = S7.GetIntAt(db3, 2);
Console.WriteLine("DB3.DBW2.0 = {0}", db3dbw2);
 
db3dbd4 = S7.GetRealAt(db3, 4);
Console.WriteLine("DB3.DBW4.0 = {0}", db3dbd4);
 
db3dbd8 = S7.GetDIntAt(db3, 8);
Console.WriteLine("DB3.DBW8.0 = {0}", db3dbd8);

WriteMultiVars

// Write multi vars
s7MultiVar = new S7MultiVar(client);
const int DB1_START_INDEX = 2;
db1 = new byte[10];
S7.SetIntAt(db1, 2 - DB1_START_INDEX, 50);
S7.SetRealAt(db1, 4 - DB1_START_INDEX, (float)36.5);
S7.SetDIntAt(db1, 8 - DB1_START_INDEX, 123456);
s7MultiVar.Add(S7Consts.S7AreaDB, S7Consts.S7WLByte, 1, DB1_START_INDEX, db1.Length, ref db1);
 
const int DB3_START_INDEX = 2;
db3 = new byte[10];
S7.SetIntAt(db3, 2 - DB3_START_INDEX, -50);
S7.SetRealAt(db3, 4 - DB3_START_INDEX, (float)-25.36);
S7.SetDIntAt(db3, 8 - DB3_START_INDEX, -123456);
s7MultiVar.Add(S7Consts.S7AreaDB, S7Consts.S7WLByte, 3, DB3_START_INDEX, db3.Length, ref db3);
result = s7MultiVar.Write();
if (result != 0)
{
    Console.WriteLine("Error on s7MultiVar.Read()");
}

References
https://www.mesta-automation.com/how-to-write-a-siemens-s7-plc-driver-with-c-and-sharp7/
https://github.com/mesta1/Sharp7-example/blob/master/Sharp7Example/Program.cs

Download image from url in C#

Simply You can use following methods.

using (WebClient client = new WebClient()) 
  {
    client.DownloadFile(new Uri(url), @"c:\temp\image35.png");

     //OR 

    client.DownloadFileAsync(new Uri(url), @"c:\temp\image35.png");
   }

If You don’t know the Format(.png, .jpeg etc) of Image

public void SaveImage(string filename, ImageFormat format) {

    WebClient client = new WebClient();
    Stream stream = client.OpenRead(imageUrl);
    Bitmap bitmap;  bitmap = new Bitmap(stream);

    if (bitmap != null) 
      bitmap.Save(filename, format);

    stream.Flush();
    stream.Close();
    client.Dispose();
}

References
https://stackoverflow.com/questions/24797485/how-to-download-image-from-url

Post data using HTTP in C#

Currently the preferred approach. Asynchronous. Ships with .NET 4.5; portable version for other platforms available via NuGet.

using System.Net.Http;

POST

using (var client = new HttpClient())
{
    var values = new Dictionary<string, string>
    {
       { "thing1", "hello" },
       { "thing2", "world" }
    };

    var content = new FormUrlEncodedContent(values);

    var response = await client.PostAsync("http://www.example.com/recepticle.aspx", content);

    var responseString = await response.Content.ReadAsStringAsync();
}

GET

using (var client = new HttpClient())
{
    var responseString = client.GetStringAsync("http://www.example.com/recepticle.aspx");
}

References
http://stackoverflow.com/questions/4015324/http-request-with-post

Keywords
http , method , get , C# , .NET