Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# copy paste an image region into another image

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);
    }
like image 955
teodron Avatar asked Mar 08 '12 11:03

teodron


1 Answers

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);                
        }
    }
like image 144
Amen Ayach Avatar answered Nov 20 '22 14:11

Amen Ayach