如何使用C#图形类绘制图像

时间:2014-10-13 05:18:46

标签: c# .net drawimage

我想使用提供的X,Y坐标和缩放值,使用C#图形类绘制图像。我试图这样做,但它没有给我正确的结果。

Stream originalStream = ImageHelper.UrlToImageStream(list1.FirstOrDefault().OriginalImageUrl);

var bmp = new Bitmap(bmp.Width, bmp.Height);

int width = 0;
int height = 0;

var img = new Bitmap(bmp,
                    (int)(bmp.Size.Width / zoomLevel),
                    (int)(bmp.Size.Height / zoomLevel));

var g = Graphics.FromImage(bmp);

g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;

g.DrawString(Text, SystemFonts.DefaultFont, Brushes.White, new Rectangle((int)CurrentTextX, (int)CurrentTextY, bmp.Width, bmp.Height));

g.DrawImage(img, new Rectangle((int)CurrentX, (int)CurrentY, bmp.Width, bmp.Height));

var stream = new System.IO.MemoryStream();

img.Save(stream, ImageFormat.Jpeg);

1 个答案:

答案 0 :(得分:0)

private void mapPaint(object sender, PaintEventArgs e)
{
    Graphics g = e.Graphics;
    Image img = Image.FromFile(@"C:\img.png");
    g.DrawImage(img, new Rectangle(10,10,img.Width/zoomLevel, img.Height/zoomLevel);
}

这样试试你现在得到的错误结果是什么?