-
March 19th, 2007, 02:40 AM
#1
How to Draw on PictureBox Image?
Hello all.
I have One PictureBox with JPEG Image. and I want to draw line on that. when i set the PictureBox's Sizemode property to Normal It draws at proper position, but when i change the Sizemode property to Stretch Image, Line cannot draw on a proper Cordinates.
I cant understand the situation. is anybody know anything about this??
here is a Sample code Which i am using Currently on mouse down
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
g = Graphics.FromImage(pictureBox1.Image);
g.DrawLine(bluePen,e.X,e.Y,e.X+100,e.Y+100);
pictureBox1.Refresh();
}
-
March 19th, 2007, 03:51 AM
#2
Re: How to Draw on PictureBox Image?
This is what happens:
1) e.X and e.Y returns the coordinates relative to the picturebox, for instance 50,50.
2) You draw a line in your graphics object starting at position 50,50
3a) Normal mode: everything is fine since the picturebox and graphics have the same scale
3b) Stretch mode: picturebox and graphics have different scale. When you draw at coordinates 50,50 in your g object, this will show at a different position than 50,50 in your picturebox since the picturebox stretches the graphics object.
Solution: you will have to calculate the stretchfactor in both x and y directions, and then transform the coordinates you use when drawing, like this:
Code:
float stretchX = (float)pictureBox1.Image.Size.Width / (float)pictureBox1.Size.Width;
float stretchY = (float)pictureBox1.Image.Size.Height/ (float)pictureBox1.Size.Height;
g = Graphics.FromImage(pictureBox1.Image);
g.DrawLine(bluePen,e.X*stretchX ,e.Y*stretchY,(e.X+100)*stretchX,(e.Y+100)*stretchY);
pictureBox1.Refresh();
(EDIT: DrawLine accepts float, not double)
Don't forget to rate
Last edited by Cyanide; March 19th, 2007 at 04:01 AM.
-
March 19th, 2007, 04:06 AM
#3
Re: How to Draw on PictureBox Image?
yes you r right. your solution works for me. thank you.
-
March 19th, 2007, 06:07 AM
#4
Re: How to Draw on PictureBox Image?
One More Question.
If I want to draw a single Dot on PictureBox.is it necessary to use DrawLine Function? or any other fucntion is there to draw a single point??
-
March 19th, 2007, 07:53 AM
#5
Re: How to Draw on PictureBox Image?
If your picturebox contains some bitmap image, it should be possible to do something like
Code:
((Bitmap)pictureBox1.Image).SetPixel(x, y, Color.Blue);
Probably you should verify that the cast executes properly, but that singel line might do the work for a simple app. Try and see if it works for you!
-
March 21st, 2007, 12:40 AM
#6
Re: How to Draw on PictureBox Image?
I already try to draw single point using drawline but wehn image is get stretched it will draw line instead of dot. it s only work in normal drawmode. I treis this bitmap but it is not drwaing anything on picture nor gives any error.
-
March 21st, 2007, 01:41 AM
#7
Re: How to Draw on PictureBox Image?
Hello
I am sorry that i forget to refresh image, i try following code and it works even for stretched images.
float stretchX = (float)pictureBox1.Image.Size.Width / (float)pictureBox1.Size.Width;
float stretchY = (float)pictureBox1.Image.Size.Height/ (float)pictureBox1.Size.Height;
Bitmap b=new Bitmap(pictureBox1.Image);
b.SetPixel(Convert.ToInt32(e.X * stretchX),Convert.ToInt32(e.Y*stretchY), Color.Blue);
pictureBox1.Image=b;
pictureBox1.Refresh();
but it takes to much time for large image (if imagesize is more than 1 mb).
so i try to use drawstring function and draw a string "." and it looks like we draw a point on picture and its qiut fast compared to above one. i really want to say thanks for the suggestion.
-
March 21st, 2007, 03:12 AM
#8
Re: How to Draw on PictureBox Image?
Originally Posted by try.test.abc
it takes to much time for large image (if imagesize is more than 1 mb)
Creating a new big bitmap may take a while, try to edit the existing bitmap directly instead, like this: (I did not test it, but it should be quicker)
Code:
float stretchX = (float)pictureBox1.Image.Size.Width / (float)pictureBox1.Size.Width;
float stretchY = (float)pictureBox1.Image.Size.Height / (float)pictureBox1.Size.Height;
Bitmap b = (Bitmap)pictureBox1.Image;
if (b!=null)
{
b.SetPixel(Convert.ToInt32(e.X * stretchX),Convert.ToInt32(e.Y*stretchY), Color.Blue);
pictureBox1.Refresh();
}
Also, the SetPixel function is well known for being terribly slow, however for just setting one pixel it should be no problem. The problems start when you try to set thousands of pixels using SetPixel.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|