我想要一个图片框或面板或....我能够添加多个图像,我能够保存图片中的所有图像,我能够清除它的所有图像! [英] i want a picturebox or panel or.... which I was able to add multiple image , I was able to save all images in a picture , I was able to clear all images from it!!!

查看:53
本文介绍了我想要一个图片框或面板或....我能够添加多个图像,我能够保存图片中的所有图像,我能够清除它的所有图像!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

图片img1 = Image.FromFile(Application.StartupPath +\\\\\+ action +.jpg);

位图bmp1 =新位图(img1, 125,25);

panel1.CreateGraphics()。DrawImage(bmp1,new Point(10,65));

bmp1.Save(@C:\新文件夹\.png,ImageFormat.Png);



以上代码对于一张图片是正确的!如果我再用其他变量写上面的代码,查看所有图像,但只保存最后一张图像....

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....

推荐答案

是的,那段代码绝对是垃圾,会导致资源泄漏。你最终会用它来崩溃Windows!



你必须调用Dispose你创建的任何Graphics对象,以及任何其他对象,例如Bitmap,它实现了IDispose接口,当你完成它。



你所说的其余部分没有多大意义。如果您想要加载一堆图像并将它们全部绘制到一个面板上,您只需加载它们并一次一个地绘制它们。



创建你的Graphics对象,然后根据需要加载你的图像并绘制它,处理你的图像,加载下一个,...,处理你的Graphics对象。



我不知道你在保存电话中做了什么。
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.


除了解决方案1:



这是正确的模式使用一次性物品:

In addition to Solution 1:

This is right pattern of using disposable objects:
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



此代码严格等同于使用try-finally块并调用 finally 块中处理

http://msdn.microsoft.com/en-us/library/yh598w02.aspx [ ^ ]。



此外,不要在你的情况下在 Graphics 中创建一个实例(你需要在极少数情况下创建它,而不是在你的情况下创建它)。相反,将作为事件参数传递的实例用于重写方法 Control.OnPaint 或事件的事件处理程序 Control.Paint 。请查看我过去的答案:

什么样的俏皮方法是Paint? (DataGridViewImageCell.Paint(...)) [ ^ ],

在面板上捕获绘图 [ ^ ],

mdi子表单之间的绘制线 [ ^ ]。



And,by顺便说一句,我的其他答案解释了为什么你几乎不应该使用控件 PictureBox

追加ap图片框内的图片 [ ^ ],

画一个C#中的矩形 [ ^ ],

如何从旧图纸中清除面板 [ ^ ]。



祝你好运。

-SA


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


这篇关于我想要一个图片框或面板或....我能够添加多个图像,我能够保存图片中的所有图像,我能够清除它的所有图像!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆