C# copy paste an image region into another image

血红的双手。 提交于 2019-12-20 18:15:14

问题


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.

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

回答1:


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


来源:https://stackoverflow.com/questions/9616617/c-sharp-copy-paste-an-image-region-into-another-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!