Problem Adapting Code to Invert a Color Image to Replace an Image in a ZoomPicBox

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have a form with a http://www.bobpowell.net/zoompicbox.htm" target="_blank
<span style="color:#0066dd ZoomPicBox in which I am trying to invert the colors of the image and replace it with a negative image<span style="text-decoration:underline into the same ZoomPicBox. I am adapting example code from
http://www.bobpowell.net/negativeimage.htm <span style="color:#0066dd http://www.bobpowell.net/negativeimage.htm . Both the ZoomPicBox and negative image code come from Bob Powells site. (Thank you, Bob.)
This is the color inversion code for the negative:
<div style="background-color:white; color:black
<pre> <span style="color:blue private Image img;

<span style="color:green // Get the image with a dialog
<span style="color:blue private <span style="color:blue void btnLoadImage_MouseClick(<span style="color:blue object sender, MouseEventArgs e)
{
OpenFileDialog dlg = <span style="color:blue new OpenFileDialog();
dlg.Filter = <span style="color:#a31515 "Image files|*.BMP;*.JPG;*.TIF;*.GIF";
<span style="color:blue if (dlg.ShowDialog() == DialogResult.OK)
{
img = Image.FromFile(dlg.FileName);
zoomPicBox2.Image = img;
}
<span style="color:blue else
Application.Exit();
}

<span style="color:green // Create the negative when the button is clicked
<span style="color:blue private <span style="color:blue void button1_MouseClick(<span style="color:blue object sender, MouseEventArgs e)
{
Bitmap copy = <span style="color:blue new Bitmap(img, img.Width, img.Height);
ImageAttributes ia = <span style="color:blue new ImageAttributes();
ColorMatrix cm = <span style="color:blue new ColorMatrix();
cm.Matrix00 = cm.Matrix11 = cm.Matrix22 = 0.99f;
cm.Matrix33 = cm.Matrix44 = 1;
cm.Matrix40 = cm.Matrix41 = cm.Matrix42 = .04f;
ia.SetColorMatrix(cm);
Graphics g = Graphics.FromImage(copy);
g.DrawImage(img, <span style="color:blue new Rectangle(0, 0, img.Width, img.Height),
0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia);
g.Dispose();
img.Dispose();
}
[/code]

... and here is the onPaint method for the ZoomPicBox:
<div style="background-color:white; color:black
<pre> <span style="color:blue protected <span style="color:blue override <span style="color:blue void OnPaint(PaintEventArgs e)
{
<span style="color:green //if no image, dont bother
<span style="color:blue if(_image==<span style="color:blue null)
{
<span style="color:blue base.OnPaintBackground(e);
<span style="color:blue return;
}
<span style="color:green //Set up a zoom matrix
Matrix mx=<span style="color:blue new Matrix(_zoom,0,0,_zoom,0,0);
<span style="color:green //now translate the matrix into position for the scrollbars
mx.Translate(<span style="color:blue this.AutoScrollPosition.X / _zoom, <span style="color:blue this.AutoScrollPosition.Y / _zoom);
<span style="color:green //use the transform
e.Graphics.Transform=mx;
<span style="color:green //and the desired interpolation mode
e.Graphics.InterpolationMode=_interpolationMode;
<span style="color:green //Draw the image ignoring the images resolution settings.
e.Graphics.DrawImage(_image,<span style="color:blue new Rectangle(0,0,<span style="color:blue this._image.Width,<span style="color:blue this._image.Height),
0,0,_image.Width, _image.Height,GraphicsUnit.Pixel);
<span style="color:blue base.OnPaint (e)
}

[/code]

The problem is that I dont know how to adapt the button click method to the onPaint event, or even if the code in the button click method actually can be put in a button click method. I think I am trying to ask, "How can I get the
Graphics g into the zoomPicBox2.Image so that the original image is replaced with the inverted image after clicking a button?"
< <hr class="sig Rob Evans
"... experience trumps brilliance..." Thomas Sowell

View the full article
 
Back
Top