Determine if two images are identical - c#

Is there any way to determine if two images are identical?
I want to change an image every time my timer ticks (animation).
But, I need to see which image is displaying, so is there any way to compare 2 images
to do what I want?
if (myImage.Flags == (Image.FromFile(#"Images/Enemy.png").Flags))
{
myImage = Image.FromFile(#"Images/Enemy2.png");
}
else
{
myImage = Image.FromFile(#"Images/Enemy.png");
}

Don't compare the images, just maintain the index of the current image in a variable.
Here's an example that works for any number of images:
private int _currentImageIndex;
private string[] _imagePaths =
{
"Images/Enemy.png",
"Images/Enemy2.png",
"Images/Enemy3.png",
};
...
void NextImage()
{
// Dispose the current image
Image img = pictureBox1.Image;
pictureBox1.Image = null;
if (img != null)
img.Dispose();
// Show the next image
_currentImageIndex = (_currentImageIndex + 1) % _imagePaths.Length;
string path = _imagePaths[_currentImageIndex];
pictureBox1.Image = Image.FromFile(path);
}

I'd try to compare ImageLocation. Although it doesn't work if you have your pictures as resources.
if (PictureBox1.ImageLocation == PictureBox2.ImageLocation)
{
}
See my question:
Dynamically changing image in a picturebox

Here goes simple answer.
In case of just 2 images, use flag
// field, true if enemy2.png is loaded
bool _image2;
// somewhere
if(_image2)
{
myImage = Image.FromFile(#"Images/Enemy.png");
_image2 = false;
}
else
{
myImage = Image.FromFile(#"Images/Enemy2.png");
_image2 = true;
}

Related

Exporting UltraExpandableGroupBox default expansion indicator

I am using a UltraExpandableGroupBox in my WinForms application. And I am using the Office2003 style with it. However, I would like to reverse the Expanded and Collapsed Indicator images used. I tried to export the images from the .isl file, but these images don't seems to be among the images exported. How do I access these images?
When the ViewStyle property for the UltraExpandableGroupBox control is set to the GroupBoxViewStyle.Office2003 the Expanded/Collapsed indicator uses an embedded bitmap.
Code below demonstrates how this bitmap can be obtained from the assembly on the runtime and can be used to reverse the current Expanded/Collapsed indicators:
private void ReverseImage_Click(object sender, EventArgs e)
{
var imageName = "GroupBox.ExpansionIndicator_Chevron.bmp";
System.IO.Stream stream = typeof(UltraExpandableGroupBox).Module.Assembly.GetManifestResourceStream(typeof(UltraExpandableGroupBox), imageName);
if (stream != null)
{
// The source bitmap has 7x10px size.
var image = Bitmap.FromStream(stream);
// Converting the image to 16x16 pixels
ultraExpandableGroupBox1.ExpansionIndicatorExpanded = ResizeImage(image, 16, 16);
// Rotation
using (var bmp = new Bitmap(image))
{
bmp.RotateFlip(RotateFlipType.Rotate180FlipNone);
image = bmp.Clone() as Image;
// Exporting bitmap to a file
bmp.Save(#".\" + imageName, ImageFormat.Bmp);
}
ultraExpandableGroupBox1.ExpansionIndicatorCollapsed = ResizeImage(image, 16, 16);
}
}
public static Image ResizeImage(Image image, int new_height, int new_width)
{
var dest = new Bitmap(new_width, new_height);
var g = Graphics.FromImage(dest);
g.InterpolationMode = InterpolationMode.High;
g.DrawImage(image, (dest.Width - image.Width)/2, (dest.Height-image.Height)/2);
return dest;
}
Exported to a file the Expanded/Collapsed indicator bitmap looks like on the picture below:
You can achieve this with simple DrawFilter. Set to your UltraExpandableGroupBox DraFilter property like this:
this.myUltraExpandableGroupBox.DrawFilter = new MyDrawFilter(expandedIndicator, collapsedInidcator);
Then create a new class named MyDrawFilter and let it inherit IUIElementDrawFilter. Your draw filter class may look like this:
public class MyDrawFilter : IUIElementDrawFilter
{
Image expandedIndicator;
Image collapsedIndicator;
public MyDrawFilter(Image expandedIndicator, Image collapsedInidcator)
{
this.expandedIndicator = expandedIndicator;
this.collapsedIndicator = collapsedInidcator;
}
public bool DrawElement(DrawPhase drawPhase, ref UIElementDrawParams drawParams)
{
if (drawParams.Element is GroupBoxExpansionIndicatorUIElement)
{
// if groupbox is expanded change the image with one provided in the constructor
// as expandedIndicator
if ((drawParams.Element.Control as UltraExpandableGroupBox).Expanded)
{
(drawParams.Element.ChildElements[0] as ImageUIElement).Image = this.expandedIndicator;
}
// else gropbox is collapsed change the image with one provided in the constructor
// as collapsedIndicator
else
{
(drawParams.Element.ChildElements[0] as ImageUIElement).Image = this.collapsedIndicator;
}
}
return false;
}
public DrawPhase GetPhasesToFilter(ref UIElementDrawParams drawParams)
{
// filter when GroupBoxExpansionIndicatorUIElement should be drawn. This element has
// one child UIElement of ImageUIElement type. This UIElement holds the expansion
// indicator image.
if (drawParams.Element is GroupBoxExpansionIndicatorUIElement)
{
// we return BeforeDrawChildeElements in order to be able to change the image
return DrawPhase.BeforeDrawChildElements;
}
return DrawPhase.None;
}
}

How to use a property two times for an image?

I'm trying to add noise to my image and show it in a pixturebox,then blur it and show it in another picturebox too. But i see two blurred image on my pictureboxes. How can i show both of them?
Note: I don't want create new Bitmap.
Filtreler f1 = new Filtreler();
Bitmap Orj = new Bitmap(pBox_SOURCE.Image);
f1.Imge = Orj;
if (SablonBoyutu % 2 == 1)
{
f1.addnoise(f1.Imge);
pictureBoxNoisyImg.Image = f1.Imge;
f1.meanfilter(SablonBoyutu, f1.Imge);
pBox_PROCESSED.Image = f1.Imge;
}
class Filtreler
{
private Bitmap resim;
public Bitmap Imge
{
get { return resim; }
set { resim = value; }
}
.... (my filters)
}
I think you need one more copy (img2) of your image
f1.addnoise(f1.Imge);
pictureBoxNoisyImg.Image = f1.Imge;
var img2 = new Bitmap(pictureBoxNoisyImg.Image);
f1.meanfilter(SablonBoyutu, img2);
pBox_PROCESSED.Image = img2;
Or
f1.addnoise(f1.Imge);
pictureBoxNoisyImg.Image = new Bitmap(f1.Imge);
f1.meanfilter(SablonBoyutu, f1.Imge);
pBox_PROCESSED.Image = f1.Imge;
Edit
To dispose old images you can do
f1.addnoise(f1.Imge);
if(pictureBoxNoisyImg.Image != null)
{
pictureBoxNoisyImg.Image.Dispose();
pictureBoxNoisyImg.Image = null;
}
pictureBoxNoisyImg.Image = new Bitmap(f1.Imge);
f1.meanfilter(SablonBoyutu, f1.Imge);
if(pBox_PROCESSED.Image != null)
{
pBox_PROCESSED.Image.Dispose();
pBox_PROCESSED.Image = null;
}
pBox_PROCESSED.Image = f1.Imge;
There is an alternative method which called Cloning (image.Clone();) instead of using new Bitmap Instance. Maybe it will help to you.
What's the difference between Bitmap.Clone() and new Bitmap(Bitmap)?

How to check if two PictureBox have the same image?

var img1 = (pictureBox1.Image = Properties.Resources.dynamite);
var img2 = (pictureBox2.Image = Properties.Resources.apple);
if (img1 != img2)
{
MessageBox.Show("ok");
pictureBox1.Image = Properties.Resources.empty;
}
else
{
MessageBox.Show("no");
Game_Over end = new Game_Over();
end.Show();
}
I want to create a simple game. If i hit a fruit the fruit picker is replace whit a empty picker and if you hit a dynamite to get game over
Convert the images into byte arrays and then do the comparison of byte arrays.
Bytearray1.SequenceEqual( bytearray2 ) can be true or false.

Image Slider in C#?

I want to create a simple image slideshow, when the timer switch, it will switch to the next index of the picturebox (and will loop) but with a fade effect. How can it be done in C#?
Current Code doesn't switch images? And also - how can I create the fade ffect?
I created a simple timer with 5,000ms interval, enabled it on start.
private void timerImage_Tick(object sender, EventArgs e)
{
if (pictureBox1.Image.Equals(InnovationX.Properties.Resources._1))
{
pictureBox1.Image = InnovationX.Properties.Resources._2;
}
else if (pictureBox1.Image.Equals(InnovationX.Properties.Resources._2))
{
pictureBox1.Image = InnovationX.Properties.Resources._3;
}
else
{
pictureBox1.Image = InnovationX.Properties.Resources._1;
}
}
You can't compare Bitmap loaded from resource in this manner. Every time you get image from resource (in your case using property InnovationX.Properties.Resources._1) you will get new instance of Bitmap class. Comparing two different instances of Bitmap classes will always result in false, even if they contains same image.
Bitmap a = InnovationX.Properties.Resources._1; // create new bitmap with image 1
Bitmap b = InnovationX.Properties.Resources._1; // create another new bitmap with image 1
bool areSameInstance = a == b; // will be false
If you load you images from resources to member variables (e.g. in Load event).
// load images when you create a form
private Bitmap image1 = InnovationX.Properties.Resources._1;
private Bitmap image2 = InnovationX.Properties.Resources._2;
private Bitmap image3 = InnovationX.Properties.Resources._3;
// assing and compare loaded images
private void timerImage_Tick(object sender, EventArgs e)
{
if (pictureBox1.Image == image1)
{
pictureBox1.Image = image2;
}
else if (pictureBox1.Image == image2)
{
pictureBox1.Image = image3;
}
else
{
pictureBox1.Image = image1;
}
}
And after that, rewrite that code using array :)
Image[] images = new {
InnovationX.Properties.Resources._1,
InnovationX.Properties.Resources._2,
InnovationX.Properties.Resources._3
};

Put a metafile or bitmap in needed picturebox

Well I have two pictureboxes. First one is for metafile image, second is for bitmap image. When the picture is in clipboard after buttom has been pressed I need to check out the format of the picture and put it in the appropriate picturebox. I did next but for some reason it doesnt work. Help me out please.
if (iData.GetDataPresent(DataFormats.MetafilePict)== true )
try
{
if (ClipboardFunctions.OpenClipboard(this.Handle))
{
if (ClipboardFunctions.IsClipboardFormatAvailable(CF_ENHMETAFILE) != 0) //CF_ENHMETAFILE=14
{
IntPtr intptr = ClipboardFunctions.GetClipboardData(CF_ENHMETAFILE);
pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox2.Image = new System.Drawing.Imaging.Metafile(intptr, true);
}
}
}
finally { ClipboardFunctions.CloseClipboard(); }
else if (iData.GetDataPresent(DataFormats.Bitmap) == true)
pictureBox1.Image = Clipboard.GetImage();

Categories