C# 将一个图像区域复制粘贴到另一个图像中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9616617/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 08:11:12  来源:igfitidea点击:

C# copy paste an image region into another image

c#imageimage-processingbitmapimage

提问by teodron

I am trying to write an utility class that permits automatic resizing of images that are tilebale. Let's say there is a srcBitmap from where I copy a region given by a Rectangle srcRegion. I then want to paste (pixel information wise) that region into another image called Bitmap destBitmap, in a destination region Rectangle destRegion. I know how to get the region from the source and put it into a Bitmap object, but I haven't yet been able to find how to actually paste a Bitmap object in a certain region, inside another, bigger Bitmap object.

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

Is there a quick way to do this? (without GDI and without delving into the byte array of the Bitmaps). Here is the snippet that should clarify my goal

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

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

采纳答案by Amen Ayach

Use this :

用这个 :

    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);                
        }
    }