码迷,mamicode.com
首页 > 其他好文 > 详细

ImageHelper

时间:2017-05-15 14:21:38      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:elf   water   setting   public   win   颜色   region   gen   宽度   

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Drawing.Drawing2D;

namespace Water.Helper
{
    /// <summary>
    /// 图片处理辅助类
    /// </summary>
    public class ImageHelper
    {
        private static int thumbnailsOutWidth = int.Parse(ConfigurationManager.AppSettings["ThumbnailsOutWidth"]);

        #region 获取缩略图对象

        /// <summary>
        /// 返回一个图片对像
        /// </summary>
        /// <param name="img">图片源</param>
        /// <param name="towidth">输出宽度</param>
        /// <param name="toheight">输出高度</param>
        /// <param name="x">图片X位置,当图片宽度小于输出图片宽度时可设置此项</param>
        /// <param name="y">图片Y位置,当图片宽度小于输出图片宽度时可设置此项</param>
        /// <param name="pw">图片宽度</param>
        /// <param name="ph">图片高度</param>
        /// <param name="bgcolor">填充背景颜色</param>
        /// <returns></returns>
        private static Bitmap getImage(Image img, int towidth, int toheight, int x, int y, int pw, int ph, Color bgcolor)
        {
            //新建一个bmp图片 
            Bitmap bitmap = new Bitmap(towidth, toheight, PixelFormat.Format32bppArgb);
            bitmap.SetResolution(img.HorizontalResolution, img.VerticalResolution);
            //新建一个画板 
            Graphics g = Graphics.FromImage(bitmap);
            //设置高质量插值法
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            //清空画布并以白色填充 
            g.Clear(bgcolor);
            //在指定位置并且按指定大小绘制原图片的指定部分 
            //g.DrawImage(img, 0, 0, towidth, toheight);
            g.DrawImage(img, new Rectangle(x, y, pw, ph), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);
            g.Dispose();
            img.Dispose();
            return bitmap;
        }

        /// <summary>
        /// 获取缩略图对象
        /// </summary>
        /// <returns></returns>
        public static Bitmap GetThumImage(Stream fileStream)
        {
            //新建一个bmp图片
            Bitmap bitmap = null;
            //获取一个图片对象
            Image img = Image.FromStream(fileStream);

            //获取原图的文件格式
            ImageFormat tFormat = ImageFormat.Jpeg;

            float YPicWidth = (float)img.Width;
            float YPicHeight = (float)img.Height;
            float OutPicWidth = (float)thumbnailsOutWidth;
            float OutPicHeight = 0f;

            //按尺寸裁切
            float ybl = YPicWidth / YPicHeight;
            //根据输出图片宽度获取缩放后图片高度
            OutPicHeight = YPicHeight * ((float)thumbnailsOutWidth / YPicWidth);
            //返回缩放后的图片再进行裁切
            bitmap = getImage(img, (int)OutPicWidth, (int)OutPicHeight, 0, 0, (int)OutPicWidth, (int)OutPicHeight, Color.White);

            //释放源对象
            img.Dispose();


            return bitmap;
        }
        #endregion

    }
}

 

ImageHelper

标签:elf   water   setting   public   win   颜色   region   gen   宽度   

原文地址:http://www.cnblogs.com/wc918/p/6855935.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!