Passing a string into a Bitmap reference
Passing a string into a Bitmap reference
Hi I'm Having trouble with the following code.
I have multiple pictureboxes in my app and would like to compare them two at a time, by using the same function.
When I run the code I get the following error "the reference is not valid, and I'm not sure where to go from here.
My Code
private void ImageCompare37()
{
Checkimage = "test";
im1 = "Pic37.Image";
imc1 = "PicComp37.Image";
MainCompare();
}
private void MainCompare()
{
int g11, g22, r11, r22, b11, b22, x1, y1, z1, x2, y2, z2;
Bitmap img1, img2;
int i, j;
img1 = new Bitmap(im1);
img2 = new Bitmap(imc1);
for (i = 0; i < img1.Width; i++)
{
for (j = 0; j < img1.Height; j++)
{
a = img1.GetPixel(i, j);
b = img2.GetPixel(i, j);
}
}
String g1 = a.G.ToString();
String g2 = b.G.ToString();
String r1 = a.R.ToString();
String r2 = b.R.ToString();
String b1 = a.R.ToString();
String b2 = b.R.ToString();
g11 = Convert.ToInt16(g1);
g22 = Convert.ToInt16(g2);
r11 = Convert.ToInt16(r1);
r22 = Convert.ToInt16(r2);
b11 = Convert.ToInt16(b1);
b22 = Convert.ToInt16(b2);
if ((g11 > g22) && (r11 > r22) && (b11 > b22))
{
x1 = g11 - g22;
y1 = r11 - r22;
z1 = b11 - b22;
}
else
{
x1 = g22 - g11;
y1 = r22 - r11;
z1 = b22 - b11;
}
if ((x1 > 20) || (y1 > 20) || (z1 > 20))
{
MatchFound = "no";
}
else
{
MatchFound = "yes";
textBox1.Text = Checkimage;
CaptureBtn.PerformClick();
}
}
How can I pass the im1
and imc
strings that allows the commands img1 = new Bitmap(im1)
img2 = new Bitmap(imc1)
to accept them.
im1
imc
img1 = new Bitmap(im1)
img2 = new Bitmap(imc1)
EDIT
The problem why I don't have fie names is because the app captures sections of the screen automatically, and places them in the pictureboxes. Using the following code.
Graphics ppcComp28 = Graphics.FromImage(PPCComp28 as Image);
ppcComp28.CopyFromScreen(ppc28x, ppc28y, 0, 0, PPCComp1.Size);
PicComp28.SizeMode = PictureBoxSizeMode.Zoom;
PicComp28.Image = PPCComp28;
Everything is local variables, is the only option to save these on the PC?
new Bitmap(im1);
im1
im1
imc1
Simply change function to
private void MainCompare(Bitmap img1, Bitmap img2)
and call it MainCompare((Bitmap)PicComp28.Image, (Bitmap)PicCompXXX.Image)
instead of im1
and imc1
– Hussein Golshani
Jul 1 at 0:46
private void MainCompare(Bitmap img1, Bitmap img2)
MainCompare((Bitmap)PicComp28.Image, (Bitmap)PicCompXXX.Image)
im1
imc1
Why are you defining so many variables in advance? Those
i
and j
ones shouldn't even exist outside the for
loops. Also, there is no use in converting integers to string and then to Int16, and then storing that in an Int32 again... especially since these values are technically all bytes anyway.– Nyerguds
Jul 1 at 13:45
i
j
for
I struggles with this for so long and it just did not want to work properly, found a basic if statement worked better than all this
– Hein
Jul 1 at 17:19
So what exactly is preventing you from simply using
img1 = Pic37.Image
and img2 = PicComp37.Image
? That should be all you need. Simply don't use strings. If those are picture boxes, you may need to cast them to Bitmap
though, since PictureBox
stores the references as Image
. All that said... what is this code actually supposed to do? I honestly can't tell.– Nyerguds
2 days ago
img1 = Pic37.Image
img2 = PicComp37.Image
Bitmap
PictureBox
Image
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
"Pic37.Image" and "PicComp37.Image" should be file name! in
new Bitmap(im1);
theim1
accepts file name. You should setim1
andimc1
to file names.– Hussein Golshani
Jul 1 at 0:27