I have a program that edits images in different ways… i have a Paint event that is called every time i do this.Invalidate()…
My paint method looks like this:
private void EditImage_Paint(object sender, PaintEventArgs e)
{
if (isLOaded == true)
{
Graphics graphicsWindow; // reference to the graphic surface of this window
Graphics graphicsImage; // reference to in-memory surface
theImage = new Bitmap(Width, Height); // bitmap for window surface copy
graphicsWindow = e.Graphics; // get our current window's surface
graphicsImage = Graphics.FromImage(theImage); // create surfaces from the bitmaps
graphicsImage.DrawImage(firstLoaded, 0, 0, Width, Height);
if (isInvert == true)
{
theImage = InvertBitmap(theImage);
}
else if (isGrayscale == true)
{
theImage = GrayscaleBitmap(theImage);
}
else if (isThreshold == true)
{
theImage = ThresholdBitmap(theImage);
}
else if (isResize == true)
{
theImage = resizeImage(theImage, 10, 100);
}
else if (isFilterRed == true)
{
theImage = FilterRedBitmap(theImage);
}
else if (isFilterGreen == true)
{
theImage = FilterGreenBitmap(theImage);
}
else if (isFilterBlue == true)
{
theImage = FilterBlueBitmap(theImage);
}
graphicsWindow.DrawImage(theImage, 0, 0);
}
}
i have another area in my code which sets some boolean values to true or false within the Click events….(as my program uses winforms) and hence my program knows which method to call. However, i think putting all of these things inside of paint is just bad design. My issue is that i don’t know how to pass in a bitmap image into a Click event? Is that possible? I would much rather deal with what happens in the Click events rather than within the paint methods. Any idea on how i could design this better?
You should keep your paint events as fast as possible.
You should store the final image in a field in the class and regenerate it when the options change.
You can then replace your entire Paint event with a simple PictureBox.
On a side note, you should either change the various Filter functions to modify the image in-place (preferred), or dispose the old image each time you create a new one.