我遗漏了一些东西。假设我有以下代码:
private Bitmap source = new Bitmap (some_stream);
Bitmap bmp = new Bitmap(100,100);
Rectangle newRect = new Rectangle(0, 0, bmp.Width, bmp.Height);
Rectangle toZoom= new Rectangle(0, 0, 10, 10);
Graphics g = Graphics.FromImage(bmp);
g.DrawImage(source, newRect, toZoom, GraphicsUnit.Pixel);我的目标是放大源图片左上角的10x10像素。在我创建了图形对象g并名为bmp之后:请求的矩形(toZoom)将被复制到bmp,或者它将显示在屏幕上?我有点糊涂了,有人能澄清一下吗?
发布于 2010-04-17 07:42:15
你的代码只会给你一个内存中的位图(它不会自动显示在屏幕上)。一种简单的显示方法是在窗体上放置一个100x100的PictureBox,并像这样设置它的Image属性(使用上面代码中的Bitmap ):
pictureBox1.Image = bmp;此外,您还需要在代码中包含一些using块:
using (private Bitmap source = new Bitmap (some_stream))
{
Bitmap bmp = new Bitmap(100,100);
Rectangle newRect = new Rectangle(0, 0, bmp.Width, bmp.Height);
Rectangle toZoom= new Rectangle(0, 0, 10, 10);
using (Graphics g = Graphics.FromImage(bmp))
{
g.DrawImage(source, newRect, toZoom, GraphicsUnit.Pixel);
}
pictureBox1.Image = bmp;
}请注意,bmp没有using块-这是因为您将其设置为PictureBox的Image属性。using块在块作用域的末尾自动调用对象的Dispose方法,这是您不想做的,因为它仍将被使用。
发布于 2010-04-17 07:21:35
它将被复制并且不显示。
https://stackoverflow.com/questions/2656479
复制相似问题