首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GDI+ DrawImage函数

GDI+ DrawImage函数
EN

Stack Overflow用户
提问于 2010-04-17 06:46:23
回答 2查看 1.8K关注 0票数 1

我遗漏了一些东西。假设我有以下代码:

代码语言:javascript
复制
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,或者它将显示在屏幕上?我有点糊涂了,有人能澄清一下吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-04-17 07:42:15

你的代码只会给你一个内存中的位图(它不会自动显示在屏幕上)。一种简单的显示方法是在窗体上放置一个100x100的PictureBox,并像这样设置它的Image属性(使用上面代码中的Bitmap ):

代码语言:javascript
复制
pictureBox1.Image = bmp;

此外,您还需要在代码中包含一些using块:

代码语言:javascript
复制
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方法,这是您不想做的,因为它仍将被使用。

票数 1
EN

Stack Overflow用户

发布于 2010-04-17 07:21:35

它将被复制并且不显示。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2656479

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档