c# picturebox bitmaps - c#

I have a picturebox that upon request, I save the current display to a bitmap.
My question is, how do I then load that same bitmap to the picturebox?
Thanks.
EDIT:
The only relevant code is:
pictureBox1.DrawToBitmap(test1,pictureBox1.ClientRectangle);
The picture box contains graphics that I have written 'freehand' using the mouse. So you can use the mouse to write directly onto the screen when the left mouse is pressed.

You can assign any image to the picture box that you want during run-time. Just set the Image property of the picture box to the picture you want to be displayed.
For example, to display an image in a picture box from a file on your hard drive, you could use something like:
myPicBox.Image = Image.FromFile("C:\savedimage.bmp");
Or, your edit suggests that you have a bitmap object in memory that you want to display in the picture box. In that case, it's a simple matter of assigning that bitmap object to the Image property:
myPicBox.Image = test1; //(where test1 is your bitmap object in memory)
Edit: Just in case you want to save the bitmap object that you created in memory to disk so that you can reload and use it later, check out the Save method of the Bitmap object:
test1.Save("C:\savedimage.bmp", System.Drawing.Imaging.ImageFormat.Bmp);

If I'm not mistaken, the picturebox should have an Image property that you can simply use to assign the Bitmap to.

Related

Save original image from PictureBox, not resized

Is it possible to save original image with original quality from PictureBox, not the stretched/resized one? I download byte array from a server and put it in PictureBox, I want user to be able to save the actual image, but it should be the original image (original quality and original size) I received from the HTTP server, not resized and reduced quality one that I show in a PictureBox. Is it possible or do I have to store the byte array I receive from server somewhere in order to achieve this?
Image property of the PictureBox contains the original image, while the control may paint the image as zoom/resized/stretched depending to its SizeMode. You can see the source code for the property here.
Just save the Image property and it will be the same image which you had read from database.
So calling pictureBox1.Image.Save will be enough.

Get all points of Image to redraw c#

Based on this post - save a graphics object to a varbinary db column c#, I created a control where I allow the user to draw on the control, and I save the drawn image to a database varbinary column. My problem is when I need to reload the signature and allow user to continue editing. I don't have all the points of the original image, so when I redraw, I only retain the user's edits.
Can somebody pls assist?
I figured it out, I was able to set the image loaded as the image of my picture box control, I used a graphics image from that loaded image. problem solved

Save Image in PictureBox with overlapping Controls

I am developing an application in which user can select an image in a picture box.
After that he can right click on the image and add a user control which will again display an Image along with some text. This user control can be added any number of times.
User can also re-position the user controls as per need.
All this functionality has been implemented and is working fine.
Now, the requirement is to save the Image along with the user control.
Above you can see the complete image which needs to be saved. Back image is the picture box image and the user control (small images with text).
When user will click on save button the image should get saved on his disk as a single image.
This is a windows application developed in C#.
I want to know that whether this functionality can be achieved or not. If yes, then please guide me in the right direction.
If you create a copy of the bitmap then with the Graphics.DrawImage() you can draw those images onto it. You need to calculate the position of those controls.
Look here for DrawImage: http://msdn.microsoft.com/en-us/library/42807xh1.aspx
example:
Bitmap copy = new Bitmap(OriginalBitmap);
Graphics g = Graphics.FromImage(copy);
g.DrawImage(arrowBitmap, new Point(..));
copy.Save(...);
A very simple and straight forward solution exists, has been thought of by Microsoft and includes these steps:
Instead of PictureBox use a Panel and instead of using the Image property of the PictureBox use the BackgroundImage property of the Panel
note: By using also the BackgroundImageLayout property you can quite easily instruct the Panel to stretch, center or zoom the image (I'm presuming the default value which is tile is not a good option in your case)
Instead of placing the other user controls at higher Z order but alongside the previous PictureBox place them inside the Panel
Use the Control.DrawToBitmap method like so:
private void button1_Click(object sender, EventArgs e) {
var bmp = new Bitmap(this.panel1.Width, this.panel1.Height);
this.panel1.DrawToBitmap(bmp, new Rectangle(Point.Empty, bmp.Size));
bmp.Save(#"D:\test.png", ImageFormat.Png);
}
That will result in your controls begin rendered along with the picture:
Furthermore, and if your scenario allows it, you could simply use the DrawToBitmap method with any control which contains all of the actors you wish to render, for instance the actual Form.

How to save Graphics created on a picture box?

I am creating a graphics object using pictureBox.CreateGraphics() method. I am using the picture box to load an image and edit it by drawing on it. I want to save the "new" image (the edited one) to a new file.
It's hard. But the tricky part is how do you do "drawing on it".

C# Remove drawing from PictureBox

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.

Categories