【问题标题】:C# copy paste an image region into another imageC#将图像区域复制粘贴到另一个图像中
【发布时间】:2012-03-25 20:59:15
【问题描述】:

我正在尝试编写一个实用程序类,该类允许自动调整平铺图像的大小。假设有一个 srcBitmap,我从中复制一个由 Rectangle srcRegion 给出的区域。然后我想将该区域粘贴(明智的像素信息)到另一个名为 Bitmap destBitmap 的图像中,在目标区域 Rectangle destRegion 中。 我知道如何从源中获取区域并将其放入 Bitmap 对象中,但我还没有找到如何将 Bitmap 对象实际粘贴到某个区域中的另一个更大的 Bitmap 对象中。

有没有快速的方法来做到这一点? (没有 GDI,也没有深入研究位图的字节数组)。这是应该阐明我的目标的sn-p

    private static void CopyRegionIntoImage(Bitmap srcBitmap, Rectangle srcRegion, Bitmap destBitmap, Rectangle destRegion)
    {
        // get the required region from the destination
        Bitmap region = Copy(srcBitmap, srcRegion);
    }

【问题讨论】:

  • 您已经将 GDI+ 与 Bitmap 类一起使用。
  • 谢谢!我猜.Net 4.0 框架在这个库周围有包装器。不知道,C# 和 dotNet 不是我的生计。

标签: c# image image-processing bitmapimage


【解决方案1】:

使用这个:

    public static void CopyRegionIntoImage(Bitmap srcBitmap, Rectangle srcRegion,ref Bitmap destBitmap, Rectangle destRegion)
    {
        using (Graphics grD = Graphics.FromImage(destBitmap))            
        {
            grD.DrawImage(srcBitmap, destRegion, srcRegion, GraphicsUnit.Pixel);                
        }
    }

【讨论】:

  • 啊,你更快更好。没见过超载xD
  • 谢谢,我也不知道过载!希望它也能帮助其他人。最好的问候!
  • 我认为您不需要“ref”键,因为您没有更改 destBitmap 对象。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-16
  • 1970-01-01
  • 2015-12-20
  • 2018-02-01
  • 2013-11-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多