由于手头这个项目需要显示数据库中存放的图片,一般我都是用GetThumbnailImage来获取的,但是通过这个方法来显示有很大的问题,如果一个图片中内嵌了缩微图,那最终显示出来的图片其实是内嵌的缩微图,换句话说有一个图片的大小是1024*768,但是它里面有内嵌的缩微图,尺寸是200*150, 所以你就会发现通过GetThumbnailImage得到的图片质量惨不忍睹,因为是用1024*768的尺寸来显示200*150的图片。
最终在网上找到了解决方案,但是很不幸,这个链接被我弄丢了,所以只好把我修改好的代码贴出来了。
你可以找一张内嵌缩微图的图来进行测试。
最终在网上找到了解决方案,但是很不幸,这个链接被我弄丢了,所以只好把我修改好的代码贴出来了。
- private void ImageShow(int maxSideSize)
- {
- // Image MimeType
- string ImageMimeType = string.Empty;
- // Get Image Data
- DataSet ds = 数据源;
- if (ds.Tables.Count == 0)
- return;
- if (ds.Tables[0].Rows.Count == 0)
- return;
- DataRow dr = ds.Tables[0].Rows[0];
- Response.Clear();
- byte[] blob = (byte[])dr["GiftPicture"];
- ImageMimeType = dr["GiftPictureMimeType"].ToString().ToLower().Trim();
- MemoryStream stream = new MemoryStream(blob);
- System.Drawing.Image image = System.Drawing.Bitmap.FromStream(stream);
- // Resize to max side size
- int width = 0;
- int height = 0;
- double ratio = 0.0;
- if ((maxSideSize < image.Width) || (maxSideSize < image.Height))
- {
- // Check larger side in image and estimate resize ratio
- if (image.Width > image.Height)
- {
- width = maxSideSize;
- ratio = Convert.ToDouble(width) / Convert.ToDouble(image.Width);
- height = Convert.ToInt32(image.Height * ratio);
- }
- else
- {
- height = maxSideSize;
- ratio = Convert.ToDouble(height) / Convert.ToDouble(image.Height);
- width = Convert.ToInt32(image.Width * ratio);
- }
- }
- else
- {
- width = image.Width;
- height = image.Height;
- }
- // Create image
- System.Drawing.Image bm = new Bitmap(width, height);
- Graphics gdiobj = Graphics.FromImage(bm);
- gdiobj.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
- gdiobj.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
- gdiobj.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
- gdiobj.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
- gdiobj.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
- Rectangle destrect = new Rectangle(0, 0, width, height);
- ImageAttributes attr = new ImageAttributes();
- attr.SetWrapMode(System.Drawing.Drawing2D.WrapMode.TileFlipXY);
- gdiobj.DrawImage(image, destrect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attr);
- // Display image for file type is jpeg or jpg.
- if (ImageMimeType.IndexOf("jpeg") > 0 || ImageMimeType.IndexOf("jpg") > 0)
- {
- // Image quality
- int mImageQuality = 100;
- ImageCodecInfo encoder = GetEncoderInfo("image/jpeg");
- // Setup the quality
- EncoderParameters encoderParameters = new EncoderParameters(1);
- encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, (long)mImageQuality);
- bm.Save(Response.OutputStream, encoder, encoderParameters);
- }
- else
- {
- bm.Save(Response.OutputStream, ImageFormat.Jpeg);
- }
- Response.ContentType = ImageMimeType;
- string fileName = System.Web.HttpUtility.UrlEncode(dr[0].ToString(), System.Text.Encoding.UTF8);
- Response.AddHeader("Content-Disposition", "inline;filename=" + fileName + ";");
- // Release source
- if (image != null)
- image.Dispose();
- if (bm != null)
- bm.Dispose();
- stream.Close();
- Response.Flush();
- Response.End();
- }
你可以找一张内嵌缩微图的图来进行测试。