---
title: "生成缩略图"
author: "ch.net"
date: "2009-01-10T10:52:22Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2009-01-10T10:52:22Z"
id: 314107
url: "https://haha-net.iteye.com/blog/314107"
---

方法一：直接读取图片文件

 

/// &lt;summary&gt;  
        /// 生成缩略图  
        /// &lt;/summary&gt;  
        /// &lt;param name="localPath"&gt;原图路径&lt;/param&gt;  
        /// &lt;param name="toWidth"&gt;缩略后的宽&lt;/param&gt;  
        /// &lt;param name="toHeight"&gt;高&lt;/param&gt;  
        /// &lt;returns&gt;&lt;/returns&gt;  
        private Image MakeThumbnail(string localPath, int toWidth, int toHeight)  
        {  
            if (!File.Exists(localPath))  
                return null; // 可以返回一个特定的图片

            Image originalImage = Image.FromFile(localPath);

            int x = 0;  
            int y = 0;  
            int ow = originalImage.Width;  
            int oh = originalImage.Height;

            //指定高宽裁减（不变形）                 
            if ((double)originalImage.Width / (double)originalImage.Height &gt; (double)toWidth / (double)toHeight)  
            {  
                oh = originalImage.Height;  
                ow = originalImage.Height * toWidth / toHeight;  
                y = 0;  
                x = (originalImage.Width - ow) / 2;  
            }  
            else  
            {  
                ow = originalImage.Width;  
                oh = originalImage.Width * toHeight / toWidth;  
                x = 0;  
                y = (originalImage.Height - oh) / 2;  
            }

            //新建一个bmp图片  
            Image bitmap = new Bitmap(toWidth, toHeight);

            //新建一个画板  
            Graphics g = Graphics.FromImage(bitmap);

            //设置高质量插值法  
            g.InterpolationMode = InterpolationMode.High;

            //设置高质量,低速度呈现平滑程度  
            g.SmoothingMode = SmoothingMode.HighQuality;

            //清空画布并以透明背景色填充  
            g.Clear(Color.Transparent);

            //在指定位置并且按指定大小绘制原图片的指定部分  
            g.DrawImage(originalImage, new Rectangle(0, 0, toWidth, toHeight),  
                new Rectangle(x, y, ow, oh),  
                GraphicsUnit.Pixel);

            originalImage.Dispose();  
            g.Dispose();  
            return bitmap;  
        }

 

 

 

方法二：读取文件到内存中

/// &lt;summary&gt;  
        /// 生成缩略图  
        /// &lt;/summary&gt;  
        /// &lt;param name="toWidth"&gt;缩略后的宽&lt;/param&gt;  
        /// &lt;param name="toHeight"&gt;高&lt;/param&gt;  
        /// &lt;param name="originalImage"&gt;原始图片&lt;/param&gt;  
        /// &lt;returns&gt;&lt;/returns&gt;  
        private Image MakeThumbnail(string localPath, int toWidth, int toHeight)  
        {  
            Image originalImage = ReadPictureStream(localPath);  
            if (originalImage == null)  
                return null;// 可以返回一个特定的图片

            int x = 0;  
            int y = 0;  
            int ow = originalImage.Width;  
            int oh = originalImage.Height;

            //指定高宽裁减（不变形）                 
            if ((double)originalImage.Width / (double)originalImage.Height &gt; (double)toWidth / (double)toHeight)  
            {  
                oh = originalImage.Height;  
                ow = originalImage.Height * toWidth / toHeight;  
                y = 0;  
                x = (originalImage.Width - ow) / 2;  
            }  
            else  
            {  
                ow = originalImage.Width;  
                oh = originalImage.Width * toHeight / toWidth;  
                x = 0;  
                y = (originalImage.Height - oh) / 2;  
            }

            //新建一个bmp图片  
            Image bitmap = new Bitmap(toWidth, toHeight);

            //新建一个画板  
            Graphics g = Graphics.FromImage(bitmap);

            //设置高质量插值法  
            g.InterpolationMode = InterpolationMode.High;

            //设置高质量,低速度呈现平滑程度  
            g.SmoothingMode = SmoothingMode.HighQuality;

            //清空画布并以透明背景色填充  
            g.Clear(Color.Transparent);

            //在指定位置并且按指定大小绘制原图片的指定部分  
            g.DrawImage(originalImage, new Rectangle(0, 0, toWidth, toHeight),  
                new Rectangle(x, y, ow, oh),  
                GraphicsUnit.Pixel);

            originalImage.Dispose();  
            g.Dispose();  
            return bitmap;  
        }

        // 读取图片存放内存中并返回一个图片对象  
        private Image ReadPictureStream(string localPath)  
        {  
            if (!File.Exists(localPath))  
                return null;  
            Image image = null;  
            FileInfo fileinfo = new FileInfo(localPath);  
            using (FileStream imgdatastream = System.IO.File.Open(localPath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite))  
            {  
                int imgdatalen = int.Parse(fileinfo.Length.ToString());   //定义每次读取字节的长度  

                byte\[] m\_ImgData = new byte\[imgdatalen];

                //获取图片的字节数    
                imgdatastream.Read(m\_ImgData, 0, imgdatalen);

                //压缩指定路径下的图片并预览    
                using (MemoryStream MemStream = new System.IO.MemoryStream(m\_ImgData))  
                {  
                    image = Image.FromStream(MemStream);  
                }  
            }  
            return image;  
        }