C#缩略图图像生成,用于大型图像出现内存不足异常 [英] C# Thumbnail Image generation for large images getting Out Of Memory exception

查看:487
本文介绍了C#缩略图图像生成,用于大型图像出现内存不足异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请找到以下代码以生成图像的缩略图版本:

 

public class ThumbnailGenerator
    {
        public void GenerateThumnail(int thumbWidth, string src, string dest)
        {

            try
            {
                using (System.Drawing.Image image = System.Drawing.Image.FromFile(src))
                {
                    int srcWidth = image.Width;
                    int srcHeight = image.Height;
                    int thumbHeight = Convert.ToInt32(Math.Round(Convert.ToDouble
(Convert.ToDouble(srcHeight) / Convert.ToDouble(srcWidth) * thumbWidth),
MidpointRounding.ToEven)); using (Bitmap bmp = new Bitmap(thumbWidth, thumbHeight)) { using (System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp)) { gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; gr.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, thumbWidth,
thumbHeight); gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel); bmp.Save(dest, System.Drawing.Imaging.ImageFormat.Jpeg); } } } } catch { throw; } } }

解决方案

Bitmap.GetThumbnailImage()也会出错吗?


Please find the following code to generate a thumbnail version of an image:

 

public class ThumbnailGenerator
    {
        public void GenerateThumnail(int thumbWidth, string src, string dest)
        {

            try
            {
                using (System.Drawing.Image image = System.Drawing.Image.FromFile(src))
                {
                    int srcWidth = image.Width;
                    int srcHeight = image.Height;
                    int thumbHeight = Convert.ToInt32(Math.Round(Convert.ToDouble
(Convert.ToDouble(srcHeight) / Convert.ToDouble(srcWidth) * thumbWidth),
MidpointRounding.ToEven)); using (Bitmap bmp = new Bitmap(thumbWidth, thumbHeight)) { using (System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp)) { gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; gr.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, thumbWidth,
thumbHeight); gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel); bmp.Save(dest, System.Drawing.Imaging.ImageFormat.Jpeg); } } } } catch { throw; } } }

解决方案

Do you also get an error with Bitmap.GetThumbnailImage()?


这篇关于C#缩略图图像生成,用于大型图像出现内存不足异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆