2011-06-03 50 views
1

我想要做的事很簡單。我想拍攝一張我已經擁有的照片,並將其粘貼到空白圖像/照片中,從而擴大了我的照片範圍。C#粘貼圖片(在圖形中)

澄清:

private static Image PasteImage(Image startimage) //start image is a square of Size(30,30) 
    { 
     //Create a new picture/graphics with size of (900,900); 
     //Paste startimage inside the created picture/graphics at Point (400,450) 
     //Return the picture/graphics which should return a square within a square 
    } 
+0

你想要的結果圖像具有對稱的邊框或不? – Dyppl 2011-06-03 03:33:14

回答

2
private static Image PasteImage(Image startimage) 
{ 
    int width = Math.Max(900, 400 + startimage.Width); 
    int height = Math.Max(900, 450 + startimage.Height); 
    var bmp = new Bitmap(width, height); 
    using (Graphics g = Graphics.FromImage(bmp)) { 
     g.DrawImage(startimage, 400, 450); 
    } 
    return bmp; 
} 

這是更好地在你的代碼擺脫常數,並添加了一些額外的PARAMS:

private static Image PasteImage(Image startimage, Size size, Point startpoint) 
{ 
    int width = Math.Max(size.Width, startpoint.X + startimage.Width); 
    int height = Math.Max(size.Height, startpoint.Y + startimage.Height); 
    var bmp = new Bitmap(width, height);   
    using (Graphics g = Graphics.FromImage(bmp)) { 
     g.Clear(Color.Black); 
     g.DrawImage(startimage, new Rectangle(startpoint, startimage.Size)); 
    } 
    return bmp; 
} 
0
  1. 從創建圖像使用以下的啓動圖像

    Graphics.FromImage(startimage);

  2. 繪製要使用

    g.DrawImage(...)的圖像