C# 如何复制 System.Drawing.Image 值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1203674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to copy a System.Drawing.Image value?
提问by Joan Venge
I am trying to copy an Image value to draw on it and on second draw ignore the last one and start over on top of the preserved Image value. I.e.:
我正在尝试复制一个 Image 值以在其上绘制,在第二次绘制时忽略最后一个并在保留的 Image 值之上重新开始。IE:
Image with 4 rectangle (ImageA)
-> draw a circle
return to ImageA
-> draw a rectangle
now there are 5 rectangles
I don't know if it's the optimal way to draw too?
我不知道这是否也是最佳的绘画方式?
回答by Blindy
You can create a new Bitmap and put a Graphics object over it, then draw ImageA over the temporary bitmap and draw your circle on it, and when you're done dispose the temporary bitmap and keep drawing on ImageA.
您可以创建一个新的 Bitmap 并在其上放置一个 Graphics 对象,然后在临时位图上绘制 ImageA 并在其上绘制圆,完成后处理临时位图并继续在 ImageA 上绘制。
回答by Blindy
I agree with Blindy. Create a new Image object and draw ontop of that while preserving your initial Image.
我同意盲人。创建一个新的 Image 对象并在其上绘制,同时保留您的初始 Image。
Bitmap myBitmap = new Bitmap("C:\<path");
Image myImage = (Image)myBitmap.Clone();
This will create a new Image object for you to then do your drawing on whilst still preserving the original image that you've loaded.
这将为您创建一个新的 Image 对象,然后在仍然保留您加载的原始图像的同时进行绘图。