C#graphics.drawImage()裁剪我的图像

时间:2013-07-16 18:04:48

标签: c# .net visual-studio-2010 drawimage

我正在尝试将图像放在更大的图像上。但是在drawImage()之后裁剪了内部图像。这是我的代码:

Bitmap im = new Bitmap("D:\\Steffen\\New folder\\I1\\I1\\" + filename + ".png");
int sizeX = im.Width;
int sizeY = im.Height;
int size = 0;
bool isWidth = false;
if (sizeX > sizeY) 
{ 
    size = sizeX; isWidth = true; 
}
else if(sizeY > sizeX)
{ 
    size = sizeY; isWidth = false; 
}

filename = filename + "New";
Bitmap bg = new Bitmap(size,size);
SolidBrush brush = new SolidBrush(Color.Aqua);

using (Graphics g = Graphics.FromImage(bg))
{
    g.FillRectangle(brush, 0, 0, size, size);
    if (isWidth==true)
    {
        g.DrawImage(im, 0, (size - sizeY) / 2);
    }
    else if(isWidth==false)
    {
        g.DrawImage(im, (size-sizeX)/2, 0);
    }
}

提前致谢

1 个答案:

答案 0 :(得分:0)

我的猜测是图像的分辨率是问题:

试试这样:

Bitmap im = new Bitmap("D:\\Steffen\\New folder\\I1\\I1\\" + filename + ".png");
im.SetResolution(96F, 96F);