C#复制将一个图像区域粘贴到另一个图像中 [英] C# copy paste an image region into another image

查看:859
本文介绍了C#复制将一个图像区域粘贴到另一个图像中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个实用程序类,该实用程序类允许自动调整图块图像的大小.假设有一个srcBitmap,我从那里复制一个Rectangle srcRegion给定的区域.然后,我想将该区域粘贴(明智地使用像素信息)到目标区域Rectangle destRegion中的另一个图像中,该图像称为Bitmap destBitmap. 我知道如何从源头获取区域并将其放入Bitmap对象,但是我还无法找到如何将Bitmap对象实际粘贴到另一个更大的Bitmap对象内的特定区域中.

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.

有没有一种快速的方法来做到这一点? (没有GDI,也无需深入研究位图的字节数组).这是应该阐明我的目标的代码段

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

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

推荐答案

使用此方法:

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

这篇关于C#复制将一个图像区域粘贴到另一个图像中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆