I have a PictureBox pic , and an Image img, pic.Image = img
and I draw some rectangles on the Image using
Graphics g = Grpahics.FromImage(pic.Image);
g.DrawRectangle(...);
But at one point, I want to remove the rectangle from the Image,I tried
pic.Image = getOriginalImage();
pic.Refresh();
but the image remains the same(Rectangles are still on top of the Image)
I know there's a method graphics.Clear(Color),but it replace the entire Image with a solid color
How do I remove the drawing only from the PictureBox?
Thanks
Edit:
I already saved the original and when I erase I use the original image
You are overwriting the original image. Save the image locally and clone it into the PictureBox.Image property. Then clone again when you want to erase
Draw the background image again using Graphics.DrawImage() or assign the image again to the Image property. Using a Graphics object in this way is writing directly to the image displayed in the PictureBox and it does not keep separate copies of what is written using a Graphics object and the background Image.
Related
So I've been struggling with this for a while. How can I turn a graphic that I drew into a bitmap and how can I keep it updated? I mean let's say I wanna draw some shapes and then go ahead and color some random areas using setPixel, getPixel etc by going pixel by pixel on the bitmap. I don't want the old drawings to dissapear, but to put the color on top of it if it makes sense. And also, how can I save the bitmap on the computer without getting just a black image?
I'm relatively new to c# and getting answers to these questions would mean the world to me.
To save a Bitmap is easy
Bitmap pic = new Bitmap(16, 16);//16x16 picture
//draw what you want
pic.Save("name.bmp");//save the drawed Bitmap
pic.Dispose();
With .Save you save the file.
With .Dispose you give the ressourses free from the picture, dispose the picture when you don't use the picture anymore.
I am working with a fingerprint module and am able to capture and display the output in a picture box (C#). Now picture.Image is null even though picturebox displays an image. So I am trying to save the picturebox image as bmp and then assign that bmp to the same picturebox so that Picturebox.image is not null.
Here is the code :
Bitmap bmp = new Bitmap(picFP.width, picFP.height);
picFP.DrawToBitmap(bmp, picFP.ClientRectangle);
bmp.Save("path", Imageformat.bmp);
picFP.image = bmp;
Here bitmap image saved is blank. What can be the problem?
A PictureBox has three layers it can display and PictureBox.DrawToBitmap will put all three things into the Bitmap:
The BackgroundImage
The Image
Any graphics created in or from the Paint event
If your bitmap comes out black then you have none of the three, or the last you have is all black.
From your description it seems as if you can display the image in the PictureBox.
So I assume that you don't display it in the right way, probably you do it like this:
using (Graphics G = picFP.CreateGraphics())
G.DrawImage(yourCapturedImage, ..)
This will not work as it only creates non-persistent graphics. These go away with e.g. each minimize-restore cycle and are not called from the DrawToBitmap call
If you really want to draw it onto the PB's surface use the Paint event! But the more natural choice would be to set the PB's Image directly:
picFP.Image = yourCapturedImage;
Update 1 As you now reveal that you don't display it yourself but simply give the control handle to the external code objNitgen=picFP.Handle; the same applies: It is that Nitgen draws only onto the surface and the result is non-persistent.
In this case the remedy is either
Taking a screenshot of the result and then work from that. Here is a post that shows you how to capture a control via screenshot..
Or you may want to check if Nitgen will draw into a bitmap directly..
For this you should be to pass it not a handle to the PictureBox but to a Bitmap instead:
private void button_Click(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(picFP.ClientSize.Width, picFP.ClientSize.Height);
Graphics G = Graphics.FromImage(bmp);
IntPtr dc= G.GetHdc();
objNitgen = dc;
objNitgen.capture();
G.ReleaseHdc(dc);
pictureBox1.Image = bmp; // now display..
bmp.Save(yourfilename); // .. and/or save
}
Update 2
You noted in a comment that doing a manual screenshot also does not capture the image; so it seems the control handle is only used to overlay it with the image much like video overlays do; if this is the case I doubt you can get at the image without using other, more fitting Nitgen SDK methods.
I have a picturebox with image, in which I have drawn a rectangular selection. I want to get the selected portion of image in another picturebox. How can I get that? Please help.
Suppose the rect is the Rectangle which you passed in the Graphics.DrawRectangle is calculated in your the coordinates of your pictureBox. You can use RectangleToScreen and RectangleToClient to get that portion in another pictureBox like this:
Rectangle portion = pictureBox2.RectagleToClient(pictureBox1.RectangleToScreen(rect));
//portion is the Rectangle calculated in the coordinates of pictureBox2.
Use coordinates of the selection to create Rectangle rectangle then:
Bitmap sourceBitmap = new Bitmap(pictureBoxImage);
Bitmap croppedBitmap = sourceBitmap.Clone(rectangle, sourceBitmap.PixelFormat);
after that you may use croppedBitmap in another picturebox. Don't forget to dispose unused images. That's all.
Currently, I'm using a picturebox inside a panel to display images. I have already implemented a logic to move image via mouse cursor.
My question is, if the size of an image is larger than the panel (picturebox's SizeMode=AutoSize), I want to crop and save the part of an image that is visible on the panel. I don't know how to do that. I'm a begginer and I don't have any experience with this.
You can use this code to crop image..
private static Image cropImage(Image img, Rectangle cropArea)
{
Bitmap bmpImage = new Bitmap(img);
Bitmap bmpCrop = bmpImage.Clone(cropArea,bmpImage.PixelFormat);
return (Image)(bmpCrop);
}
Also see these tutorial..
http://www.switchonthecode.com/tutorials/csharp-tutorial-image-editing-saving-cropping-and-resizing
http://jasonjano.wordpress.com/2010/02/13/image-resizing-and-cropping-in-c/
How to draw a rectangle on picturebox and save selected part as .jpg.
Well i know how to draw a rectange on picturebox, but i don't know how part of picture inside this rectangle can be saved as for example new .jpg..
If your image is always perfectly fited in picture box, you can create Graphics from Image class
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.fromimage.aspx
And after use Graphics http://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawimageunscaledandclipped.aspx DrawImageUnscaledAndCliped method to clip your image in memory. After this you can save that Image to file.
If image is not fitted so you use SizeMode picturebox property, you need to calculate a proportion of drawn rectangle in regard of picture box rectangle and after apply that proportion to image real rectangle.