C#粘贴图片(图形中)

时间:2011-06-03 03:04:07

标签: c# graphics image-processing

我想做的事情很简单。我想拍一张我已经拥有的照片并将其粘贴到某个空白的图形/图片中,从而扩大了我的照片范围。

澄清:

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
    }

2 个答案:

答案 0 :(得分: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;
}

最好在代码中删除常量并添加几个附加参数:

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

答案 1 :(得分:0)

  1. 使用以下

    从startimage创建图像

    Graphics.FromImage(startimage);

  2. 将图像绘制到您想要使用的位置

    g.DrawImage(...)