我将一些背景图像存储为二进制数据。我需要根据数据在其上绘制一些内容,然后在浏览器中将其显示为单个图像。问题是,当我尝试使用以下代码执行此操作时,某些图像会被放大并且有些图像会缩小。谁能告诉我哪里出错了?
int imageWidth = 0, imageHeight=0;
Image bmpImg;
if (datatable.Rows.Count > 0)
{
bmpImg = Bitmap.FromStream(new MemoryStream((byte[])datatable.Rows[0]["data"]));
imageWidth = bmpImg.Width;
imageHeight= bmpImg.Height;
}
else{
bmpImg = null;
}
bitmap = new Bitmap(imageWidth, imageHeight);
//bitmap = new Bitmap(1000, 800);
renderer = SvgRenderer.FromImage(bitmap);
graphics = Graphics.FromImage(bitmap);
if(bmpImg != null)
{
graphics.DrawImage(bmpImg, 0, 0);
}
//perform other drawings using graphics
MemoryStream ms = new MemoryStream();
bitmap.Save(ms, ImageFormat.Png);
bitmap.Dispose();
renderer.Dispose();
答案 0 :(得分:1)
经过一番搜索,我得到了另一个问题的答案:Graphics.DrawImage unexpectedly resizing image
基本上你必须改变这个:
graphics.DrawImage(bmpImg,0,0);
到
graphics.DrawImage(bmpImg,new Rectangle(0,0,imageWidth,imageHeight));