Click here to Skip to main content
15,885,182 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
Image img1 = Image.FromFile(Application.StartupPath + "\\pic\\" + action + ".jpg");
Bitmap bmp1 = new Bitmap(img1, 125, 25);
panel1.CreateGraphics().DrawImage(bmp1, new Point(10, 65));
bmp1.Save(@"C:\New folder\.png", ImageFormat.Png);

above code is correct just for one image!!!! if i write above code again with other variable, view all image but just saved last image....
Posted
Comments
[no name] 14-Sep-14 13:43pm    
What kind of filename is ".png"? Maybe you should supply a filename for each image you want to save instead of using the same one for all of them.
Sergey Alexandrovich Kryukov 14-Sep-14 21:34pm    
Strictly speaking, this is a valid filename. Anyway, the code makes no sense, of course.
—SA

Yeah, that code is absolutely garbage and will lead to resource leaks. You WILL eventually crash Windows with it!

You MUST call Dispose an any Graphics object you create, as well as any other object, such as that Bitmap, that implements the IDispose interface, when you're done with it.

The rest of what you're saying doesn't make much sense. If you're looking to load a bunch of images and paint them all onto a single Panel, you simply have to load them and paint them one at a time.

Create your Graphics object, then load your image and paint it however you require, dispose your image, load the next, ..., dispose your Graphics object.

I have no idea what you're doing with the Save call.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 14-Sep-14 23:20pm    
Agree, but there is a lot more to it (voted 4). First of all, OP needs to understand using statement.
Please see my answer.
—SA
Dave Kreskowiak 15-Sep-14 7:21am    
Yeah, I was in a bit of a rush last night.
In addition to Solution 1:

This is right pattern of using disposable objects:
C#
using (Bitmap someBitmap = new Bitmap(/* ... */)) {
   Graphics graphics = //... by the way, don't create graphics instance, see below
   // ...
   graphics.DrawImage(someBitmap, /* ... */);
   // ...
} // someBitmap.Dispose() is automatically called here
  // even if exception is thrown

This code is strictly equivalent to using try-finally block with calling Dispose in the finally block:
http://msdn.microsoft.com/en-us/library/yh598w02.aspx[^].

Besides, don't create an instance in of the Graphics in your case (you need to create it in very rare cases, not in yours). Instead, use the instance passed as a event argument parameter to the overridden method Control.OnPaint or your event handler of the event Control.Paint. Please see my past answers:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
capture the drawing on a panel[^],
Drawing Lines between mdi child forms[^].

And, by the way, my other answers explain why you hardly should use the control PictureBox:
Append a picture within picturebox[^],
draw a rectangle in C#[^],
How do I clear a panel from old drawing[^].

Good luck.
—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900