首页 > 其他 > 详细

发一些自己最常用的C#函数(二) :上传图片处理

时间:2014-04-02 02:58:47      阅读:632      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace ZooleeBasic
{
    public class ZImage
    {
        private static readonly string[] ImageFileExtensions = { ".bmp", ".gif", ".jpg", ".jpeg", ".png" };
        private static readonly ImageFormat[] ImageFormats = { ImageFormat.Bmp, ImageFormat.Gif, ImageFormat.Jpeg, ImageFormat.Jpeg, ImageFormat.Png };

        /// <summary>
        /// 缩放模式
        /// </summary>
        public enum ResizeMod
        {
            定宽定高简单缩放 = 1,
            定宽等比例缩放 = 2,
            定高等比例缩放 = 3,
            定宽定高等比例缩放 = 4
        }

        /// <summary>
        /// 水印位置
        /// </summary>
        public enum WatermarkPosition
        {
            左上 = 1,
            左中 = 2,
            左下 = 3,
            中上 = 4,
            正中 = 5,
            中下 = 6,
            右上 = 7,
            右中 = 8,
            右下 = 9
        }

        /// <summary>
        /// 是否图像文件
        /// </summary>
        /// <param name="filePath">文件的物理路径</param>
        /// <returns></returns>
        public static bool IsImageFile(string filePath) 
        {
            string ext = ZFile.GetFileExtension(filePath);
            if(ZString.IsInArray(ext,ImageFileExtensions,false)){return true;}
            return false;
        }

        /// <summary>
        /// 获得一个文件的图像文件格式(非图像文件返回null)
        /// </summary>
        /// <param name="filePath">文件的物理路径</param>
        /// <returns>返回图像文件格式</returns>
        public static ImageFormat GetImageFormat(string filePath) 
        {
            string ext = ZFile.GetFileExtension(filePath);
            int id = ZString.GetIDInArray(ext, ImageFileExtensions, false);
            if (id < 0) { return null; }
            return ImageFormats[id];
        }

        /// <summary>
        /// 缩放图像文件
        /// </summary>
        /// <param name="oFilePath">源文件的物理路径</param>
        /// <param name="toFilePath">新文件的物理路径</param>
        /// <param name="toWidth">新的图像宽度</param>
        /// <param name="toHeight">新的图像高度</param>
        /// <param name="resizeMod">缩放模式</param>   
        public static void Resize(string oFilePath, string toFilePath, int toWidth, int toHeight, ResizeMod resizeMod)
        {
            if(!IsImageFile(oFilePath)){ return; }
            Image oImage = Image.FromFile(oFilePath);
            int ow = oImage.Width;
            int oh = oImage.Height;
            int tow = toWidth;
            int toh = toHeight;
            int x = 0;
            int y = 0;
            switch (resizeMod)
            {
                case ResizeMod.定宽定高简单缩放:              
                    break;
                case ResizeMod.定宽等比例缩放:                   
                    toh = oImage.Height * toWidth / oImage.Width;
                    break;
                case ResizeMod.定高等比例缩放:
                    tow = oImage.Width * toHeight / oImage.Height;
                    break;
                case ResizeMod.定宽定高等比例缩放:                
                    if ((double)oImage.Width / (double)oImage.Height > (double)tow / (double)toh)
                    {
                        oh = oImage.Height;
                        ow = oImage.Height * tow / toh;
                        y = 0;
                        x = (oImage.Width - ow) / 2;
                    }
                    else
                    {
                        ow = oImage.Width;
                        oh = oImage.Width * toHeight / tow;
                        x = 0;
                        y = (oImage.Height - oh) / 2;
                    }
                    break;
                default:
                    break;
            }            
            Image bitmap = new Bitmap(tow, toh);//新建一个bmp图片            
            Graphics g = Graphics.FromImage(bitmap);//新建一个画板            
            g.InterpolationMode = InterpolationMode.High;//设置高质量插值法            
            g.SmoothingMode = SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度            
            g.Clear(System.Drawing.Color.Transparent);//清空画布并以透明背景色填充            
            g.DrawImage(oImage, new Rectangle(0, 0, tow, toh), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel);//在指定位置并且按指定大小绘制原图片的指定部分
            try
            {   
                ZFile.CreateDirectory(ZFile.GetFileDiretory(toFilePath));
                bitmap.Save(toFilePath, GetImageFormat(oFilePath));//以原图片格式保存缩放图
            }
            catch (System.Exception e)
            {
                throw e;
            }
            finally
            {
                oImage.Dispose();
                bitmap.Dispose();
                g.Dispose();
            }
        }

        /// <summary>
        /// 加图片水印
        /// </summary>
        /// <param name="oFilePath">源文件的物理路径</param>
        /// <param name="toFilePath">新文件的物理路径</param>
        /// <param name="watermarkPath">水印图片的物理路径</param>
        /// <param name="watermarkPosition">水印位置</param>
        public static void ImageWatermark(string oFilePath, string toFilePath, string watermarkPath, WatermarkPosition watermarkPosition)
        {
            string ext = ZFile.GetFileExtension(oFilePath);
            if (!ZString.IsInArray(ext, ImageFileExtensions, false)) { return; }
            int x = 0;
            int y = 0;
            Image img = Bitmap.FromFile(oFilePath);
            Image waterimg = Image.FromFile(watermarkPath);
            switch(watermarkPosition)
            {
                case WatermarkPosition.左上:
                    x=10;
                    y=10;
                    break;
                case WatermarkPosition.左中:
                    x = 10;
                    y = img.Height / 2 - waterimg.Height / 2;
                    break;
                case WatermarkPosition.左下:
                    x = 10;
                    y = img.Height - waterimg.Height - 10;
                    break;
                case WatermarkPosition.中上:
                    x = img.Width / 2 - waterimg.Width / 2;
                    y = 10;
                    break;
                case WatermarkPosition.正中:
                    x = img.Width / 2 - waterimg.Width / 2;
                    y = img.Height / 2 - waterimg.Height / 2;
                    break;
                case WatermarkPosition.中下:
                    x = img.Width / 2 - waterimg.Width / 2;
                    y = img.Height - waterimg.Height -10;
                    break;
                case WatermarkPosition.右上:
                    x = img.Width - waterimg.Width - 10;
                    y = 10;
                    break;
                case WatermarkPosition.右中:
                    x = img.Width - waterimg.Width - 10;
                    y = img.Height / 2 - waterimg.Height / 2;
                    break;
                case WatermarkPosition.右下:
                    x = img.Width - waterimg.Width - 10;
                    y = img.Height - waterimg.Height - 10;
                    break;
                default:
                    break;            
            }
            Graphics g = Graphics.FromImage(img);
            g.DrawImage(waterimg, new Rectangle(x, y, waterimg.Width, waterimg.Height));
            try
            {
                ZFile.CreateDirectory(ZFile.GetFileDiretory(toFilePath));
                img.Save(toFilePath, GetImageFormat(oFilePath));
            }
            catch (Exception)
            {
                throw;
            }
            finally 
            {
                waterimg.Dispose();
                img.Dispose();
                g.Dispose();
            }
        }

        /// <summary>
        /// 加文字水印
        /// </summary>
        /// <param name="oFilePath">源文件的物理路径</param>
        /// <param name="toFilePath">新文件的物理路径</param>
        /// <param name="letter">水印文字</param>
        /// <param name="font">水印文字的文本格式</param>
        /// <param name="brush">水印文字的画刷</param>
        /// <param name="watermarkPosition">水印位置</param>
        public static void LetterWatermark(string oFilePath, string toFilePath, string letter, Font font, Brush brush, WatermarkPosition watermarkPosition)
        {
            string ext = ZFile.GetFileExtension(oFilePath);
            if (!ZString.IsInArray(ext, ImageFileExtensions, false)) { return; }
            float x = 0;
            float y = 0;
            Image img = Bitmap.FromFile(oFilePath);
            float len = ZString.GetStringLength(letter);
            float size = font.GetHeight();
            float letterLength = len * size;
            float letterHeight = size;
            switch (watermarkPosition)
            {
                case WatermarkPosition.左上:
                    x = 10;
                    y = 10;
                    break;
                case WatermarkPosition.左中:
                    x = 10;
                    y = img.Height / 2 - letterHeight / 2;
                    break;
                case WatermarkPosition.左下:
                    x = 10;
                    y = img.Height - letterHeight - 10;
                    break;
                case WatermarkPosition.中上:
                    x = img.Width / 2 - letterLength / 2;
                    y = 10;
                    break;
                case WatermarkPosition.正中:
                    x = img.Width / 2 - letterLength / 2;
                    y = img.Height / 2 - letterHeight / 2;
                    break;
                case WatermarkPosition.中下:
                    x = img.Width / 2 - letterLength / 2;
                    y = img.Height - letterHeight - 10;
                    break;
                case WatermarkPosition.右上:
                    x = img.Width - letterLength - 10;
                    y = 10;
                    break;
                case WatermarkPosition.右中:
                    x = img.Width - letterLength - 10;
                    y = img.Height / 2 - letterHeight / 2;
                    break;
                case WatermarkPosition.右下:
                    x = img.Width - letterLength - 10;
                    y = img.Height - letterHeight - 10;
                    break;
                default:
                    break;
            }
            Graphics g = Graphics.FromImage(img);
            g.DrawString(letter, font, brush, x, y);
            try
            {
                ZFile.CreateDirectory(ZFile.GetFileDiretory(toFilePath));
                img.Save(toFilePath, GetImageFormat(oFilePath));
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                img.Dispose();
                g.Dispose();
                brush.Dispose();
                font.Dispose();
            }
        }



    }
}
bubuko.com,布布扣

发一些自己最常用的C#函数(二) :上传图片处理,布布扣,bubuko.com

发一些自己最常用的C#函数(二) :上传图片处理

原文:http://www.cnblogs.com/apollosun123/p/3639591.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!