日期:2014-05-20  浏览次数:21014 次

c#获取本地.png文件输出页面上背景是黑的
我读取图片文件 输出到页面上,可是我读取.png 图片文件 然后在页面上绘画输出出来的时候,.png 后面的背景就成了黑色 这是怎么回事。
  代码如下:
 System.Drawing.Image image=System.Drawing.Image.FromFile(strImageUrl);
 Graphics g = Graphics.FromImage(image);  
 g = Graphics.FromImage(image);
 g.DrawRectangle(new Pen(System.Drawing.Color.White, 0), 0, 0, image.Width + 1, image.Height + 1);
 //输出到浏览器
 System.IO.MemoryStream ms = new System.IO.MemoryStream();
 image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
 HttpContext.Current.Response.ClearContent();
 HttpContext.Current.Response.ContentType = "image/Jpeg";
 HttpContext.Current.Response.BinaryWrite(ms.ToArray());
 g.Dispose();
 image.Dispose();


  图片在这 :

------解决方案--------------------
protected void Page_Load(object sender, EventArgs e)
{
string url = Server.MapPath("images/11.png");
System.Drawing.Image image = System.Drawing.Image.FromFile(url);
//Graphics g = Graphics.FromImage(image);
//g.DrawRectangle(new Pen(System.Drawing.Color.White, 0), 0, 0, image.Width + 1, image.Height + 1);
////输出到浏览器
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ContentType = "image/Png";
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
//g.Dispose();
image.Dispose();
}