|
You last visited: Today at 06:24
Advertisement
C# ImageSearchDll
Discussion on C# ImageSearchDll within the .NET Languages forum part of the Coders Den category.
03/25/2011, 14:31
|
#1
|
elite*gold: 0
Join Date: Mar 2010
Posts: 2
Received Thanks: 0
|
C# ImageSearchDll
Hy Guys.
I want to use ImageSearch in VS(2008 or 2010) C#
I have found this, but this is written for VB:
But i have found this code(below) with google, but this throw me an exception, when i "click" on button, and try to search an image: "System.ExecutionEngineException"
If anybody have a solution, please share with me, thanks.
Any help would be appreciated.
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace ImageSearch
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int[] img = IMGSearch.FindImage(0, 0, 1366, 768, "C:\\s.bmp");
if (img[0] != 0)
{
MessageBox.Show("Img found", "My Application", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
}
}
}
public static class IMGSearch
{
//[DllImport("ImageSearchDLL.dll", EntryPoint="ImageSearch")]
[DllImport("ImageSearchDLL.dll")]
public static extern string ImageSearch(int x1, int y1, int x2, int y2, string str);
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main2()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
public static int[] FindImage(int L, int T, int R, int B, string F)
{
int[] aRTN = new int[5];
//MessageBox.Show(R.ToString());
string rtn = ImageSearch(L, T, R, B, F);
//MessageBox.Show(rtn);
if (rtn == "0")
{
aRTN[0] = 0;
return aRTN;
}
else
{
string[] coords = rtn.Split('|'); //split return value of imagesearch into array
//int[] aRTN = new int[5]; //declare int array with enough elements
aRTN[0] = int.Parse(coords[0]); //convert the string values into ints
aRTN[1] = int.Parse(coords[1]);
aRTN[2] = int.Parse(coords[2]);
aRTN[3] = int.Parse(coords[3]);
aRTN[4] = int.Parse(coords[4]);
return aRTN;
}
}
}
}
|
|
|
03/25/2011, 22:31
|
#2
|
elite*gold: 0
Join Date: May 2010
Posts: 793
Received Thanks: 268
|
Quote:
Originally Posted by Gergo87
Hy Guys.
I want to use ImageSearch in VS(2008 or 2010) C#
I have found this, but this is written for VB:
But i have found this code(below) with google, but this throw me an exception, when i "click" on button, and try to search an image: "System.ExecutionEngineException"
If anybody have a solution, please share with me, thanks.
Any help would be appreciated.
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace ImageSearch
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int[] img = IMGSearch.FindImage(0, 0, 1366, 768, "C:\\s.bmp");
if (img[0] != 0)
{
MessageBox.Show("Img found", "My Application", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
}
}
}
public static class IMGSearch
{
//[DllImport("ImageSearchDLL.dll", EntryPoint="ImageSearch")]
[DllImport("ImageSearchDLL.dll")]
public static extern string ImageSearch(int x1, int y1, int x2, int y2, string str);
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main2()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
public static int[] FindImage(int L, int T, int R, int B, string F)
{
int[] aRTN = new int[5];
//MessageBox.Show(R.ToString());
string rtn = ImageSearch(L, T, R, B, F);
//MessageBox.Show(rtn);
if (rtn == "0")
{
aRTN[0] = 0;
return aRTN;
}
else
{
string[] coords = rtn.Split('|'); //split return value of imagesearch into array
//int[] aRTN = new int[5]; //declare int array with enough elements
aRTN[0] = int.Parse(coords[0]); //convert the string values into ints
aRTN[1] = int.Parse(coords[1]);
aRTN[2] = int.Parse(coords[2]);
aRTN[3] = int.Parse(coords[3]);
aRTN[4] = int.Parse(coords[4]);
return aRTN;
}
}
}
}
|
do you have a dll called "ImageSearchDLL"? because in your code is "[DllImport("ImageSearchDLL.dll")]" that mean you should have one.
but it is really easy to write a ImageSearch on you own, you can make a screenshot:
Code:
Bitmap bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
Graphics gfxScreenshot = Graphics.FromImage(bmpScreenshot);
// Take the screenshot from the upper left corner to the right bottom corner
gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
and load the image you want to search from a file:
Code:
Bitmap bmpSearch = new Bitmap("aImage.bmp");
to Compare these Bitmaps you can for example use a for-loop and Bitmap.GetPixel(int x, int y) and to find the right point on the screen.
|
|
|
03/25/2011, 22:55
|
#3
|
elite*gold: 0
Join Date: Mar 2010
Posts: 2
Received Thanks: 0
|
Quote:
Originally Posted by nkkk
do you have a dll called "ImageSearchDLL"? because in your code is "[DllImport("ImageSearchDLL.dll")]" that mean you should have one.
but it is really easy to write a ImageSearch on you own, you can make a screenshot:
Code:
Bitmap bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
Graphics gfxScreenshot = Graphics.FromImage(bmpScreenshot);
// Take the screenshot from the upper left corner to the right bottom corner
gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
and load the image you want to search from a file:
Code:
Bitmap bmpSearch = new Bitmap("aImage.bmp");
to Compare these Bitmaps you can for example use a for-loop and Bitmap.GetPixel(int x, int y) and to find the right point on the screen.
|
Yes, dll name is ImageSearchDLL.dll.
Thanks for answer, i will try it.
|
|
|
All times are GMT +1. The time now is 06:25.
|
|