2012-03-08 305 views
11

我想寫一個實用程序類,允許tilebale圖像的自動調整大小。假設有一個srcBitmap,我從中複製了由Rectangle srcRegion給出的區域。然後,我想將該區域粘貼(像素信息明智)到另一個名爲Bitmap destBitmap的圖像中,位於目標區域Rectangle destRegion中。 我知道如何從源代碼獲取區域並將其放入Bitmap對象,但我還沒有找到如何實際將Bitmap對象粘貼到另一個較大的Bitmap對象的某個區域中。C#複製粘貼圖像區域到另一個圖像

有沒有快速的方法來做到這一點? (沒有GDI,也沒有鑽研Bitmaps的字節數組)。下面是應該明確自己的目標

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

您已經在使用GDI +與Bitmap類。 – dvdvorle 2012-03-08 11:10:38

+0

謝謝!我想.NET 4.0框架在這個庫的周圍有包裝。沒有意識到它,C#和dotNet不是我的麪包和黃油。 – teodron 2012-03-08 12:01:35

回答

19

使用片斷這樣的:

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

啊,你越快越好。沒有看到過載xD – dvdvorle 2012-03-08 11:19:53

+0

謝謝,我也沒有意識到過載!希望它也能幫助其他人。最好的祝福! – teodron 2012-03-08 13:04:37

+0

+1,不客氣 – 2012-03-08 13:24:02