2011-06-03 47 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(...)的图像