• 各式各样的图片缩略水印静态类


    各种各式各样的图片缩略水印静态,注释写的不多
    复制  保存
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    using System.Text.RegularExpressions;
    namespace Steam.Core
    {
    public static class ImageDeal
    {
    /// <summary>
            /// 缩略图,按高度和宽度来缩略
            ///http://www.cnblogs.com/pooeo/
            /// </summary>
            /// <param name="image"></param>
            /// <param name="size"></param>
            /// <returns></returns>
            public static Image Scale(Image image, Size size)
    {
    return image.GetThumbnailImage(size.Width, size.Height, null, new IntPtr());
    }
    /// <summary>
            /// 缩略图,按倍数来缩略
            ///http://www.cnblogs.com/pooeo/
            /// </summary>
            /// <param name="image">原图</param>
            /// <param name="multiple">放大或缩小的倍数,负数表示缩小,正数表示放大</param>
            /// <returns></returns>
            public static Image Scale(Image image, Int32 multiple)
    {
    Int32 newWidth;
    Int32 newHeight;
    Int32 absMultiple = Math.Abs(multiple);
    if (multiple == 0)
    {
    return image.Clone() as Image;
    }
    if (multiple < 0)
    {
    newWidth = image.Width / absMultiple;
    newHeight = image.Height / absMultiple;
    }
    else
    {
    newWidth = image.Width * absMultiple;
    newHeight = image.Height * absMultiple;
    }
    return image.GetThumbnailImage(newWidth, newHeight, null, new IntPtr());
    }
    /// <summary>
            /// 固定宽度缩略
            ///http://www.cnblogs.com/pooeo/
            /// </summary>
            /// <param name="image"></param>
            /// <param name="width"></param>
            /// <returns></returns>
            public static Image ScaleFixWidth(Image image, Int32 width)
    {
    Int32 newWidth = width;
    Int32 newHeight;
    Double tempMultiple = (Double) newWidth / (Double) image.Width;
    newHeight = (Int32) (((Double) image.Height) * tempMultiple);
    Image newImage = new Bitmap(newWidth, newHeight);
    using (Graphics newGp = Graphics.FromImage(newImage))
    {
    newGp.CompositingQuality = CompositingQuality.HighQuality;
    //设置高质量插值法
                    newGp.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
    //设置高质量,低速度呈现平滑程度
                    newGp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    //清空画布并以透明背景色填充
                    newGp.Clear(Color.Transparent);
    newGp.DrawImage(image, new Rectangle(0, 0, newWidth, newHeight));
    }
    return newImage;
    }
    /// <summary>
            /// 固定高度缩略
            ///http://www.cnblogs.com/pooeo/
            /// </summary>
            /// <param name="image"></param>
            /// <param name="height"></param>
            /// <returns></returns>
            public static Image ScaleFixHeight(Image image, Int32 height)
    {
    Int32 newWidth;
    Int32 newHeight = height;
    Double tempMultiple = (Double) newHeight / (Double) image.Height;
    newWidth = (Int32) (((Double) image.Width) * tempMultiple);
    Image newImage = new Bitmap(newWidth, newHeight);
    using (Graphics newGp = Graphics.FromImage(newImage))
    {
    newGp.CompositingQuality = CompositingQuality.HighQuality;
    //设置高质量插值法
                    newGp.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
    //设置高质量,低速度呈现平滑程度
                    newGp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    //清空画布并以透明背景色填充
                    newGp.Clear(Color.Transparent);
    newGp.DrawImage(image, new Rectangle(0, 0, newWidth, newHeight));
    }
    return newImage;
    }
    /// <summary>
            /// 裁减缩略,根据固定的高度和宽度
            ///http://www.cnblogs.com/pooeo/
            /// </summary>
            /// <param name="image"></param>
            /// <param name="width"></param>
            /// <param name="heigth"></param>
            /// <returns></returns>
            public static Image ScaleCut(Image image, Int32 width, Int32 height)
    {
    int x = 0;
    int y = 0;
    int ow = image.Width;
    int oh = image.Height;
    if (width >= ow && height >= oh)
    {
    return image;
    }
    //如果结果要比原来的宽
                if (width > ow)
    {
    width = ow;
    }
    if (height > oh)
    {
    height = oh;
    }
    if ((double) image.Width / (double) image.Height > (double) width / (double) height)
    {
    oh = image.Height;
    ow = image.Height * width / height;
    y = 0;
    x = (image.Width - ow) / 2;
    }
    else
    {
    ow = image.Width;
    oh = image.Width * height / width;
    x = 0;
    y = (image.Height - oh) / 2;
    }
    Image newImage = new Bitmap(width, height);
    using (Graphics newGp = Graphics.FromImage(newImage))
    {
    newGp.CompositingQuality = CompositingQuality.HighQuality;
    //设置高质量插值法
                    newGp.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
    //设置高质量,低速度呈现平滑程度
                    newGp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    //清空画布并以透明背景色填充
                    newGp.Clear(Color.Transparent);
    newGp.DrawImage(image, new Rectangle(0, 0, width, height),
    new Rectangle(x, y, ow, oh),
    GraphicsUnit.Pixel);
    }
    return newImage;
    }
    /// <summary>
            /// 生成缩略图
            ///http://www.cnblogs.com/pooeo/
            /// </summary>
            /// <param name="originalImagePath">源图路径(物理路径)</param>
            /// <param name="thumbnailPath">缩略图路径(物理路径)</param>
            /// <param name="width">缩略图宽度</param>
            /// <param name="height">缩略图高度</param>
            /// <param name="mode">生成缩略图的方式</param>
            public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
    {
    Image originalImage = Image.FromFile(originalImagePath);
    int towidth = width;
    int toheight = height;
    int x = 0;
    int y = 0;
    int ow = originalImage.Width;
    int oh = originalImage.Height;
    switch (mode)
    {
    case "HW"://指定高宽缩放(可能变形)
                    break;
    case "W"://指定宽,高按比例
                    toheight = originalImage.Height * width / originalImage.Width;
    break;
    case "H"://指定高,宽按比例
                    towidth = originalImage.Width * height / originalImage.Height;
    break;
    case "Cut"://指定高宽裁减(不变形)
                    if ((double) originalImage.Width / (double) originalImage.Height > (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 * height / towidth;
    x = 0;
    y = (originalImage.Height - oh) / 2;
    }
    break;
    default:
    break;
    }
    //新建一个bmp图片
                Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
    //新建一个画板
                Graphics g = System.Drawing.Graphics.FromImage(bitmap);
    //设置高质量插值法
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
    //设置高质量,低速度呈现平滑程度
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    //清空画布并以透明背景色填充
                g.Clear(Color.Transparent);
    //在指定位置并且按指定大小绘制原图片的指定部分
                g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
    new Rectangle(x, y, ow, oh),
    GraphicsUnit.Pixel);
    try
    {
    //以jpg格式保存缩略图
                    bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
    }
    catch (System.Exception e)
    {
    throw e;
    }
    finally
    {
    originalImage.Dispose();
    bitmap.Dispose();
    g.Dispose();
    }
    }
    /// <summary>
            /// 打水印,在某一点
            ///http://www.cnblogs.com/pooeo/
            /// </summary>
            /// <param name="image"></param>
            /// <param name="waterImagePath"></param>
            /// <param name="p"></param>
            public static void Makewater(Image image, String waterImagePath, Point p)
    {
    ImageDeal.Makewater(image, waterImagePath, p, ImagePosition.TopLeft);
    }
    public static void Makewater(Image image, String waterImagePath, Point p, ImagePosition imagePosition)
    {
    using (Image warterImage = Image.FromFile(waterImagePath))
    {
    using (Graphics newGp = Graphics.FromImage(image))
    {
    newGp.CompositingQuality = CompositingQuality.HighQuality;
    //设置高质量插值法
                        newGp.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
    //设置高质量,低速度呈现平滑程度
                        newGp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    switch (imagePosition)
    {
    case ImagePosition.BottomLeft:
    p.Y = image.Height - warterImage.Height - p.Y;
    break;
    case ImagePosition.TopRigth:
    p.X = image.Width - warterImage.Width - p.X;
    break;
    case ImagePosition.BottomRight:
    p.Y = image.Height - warterImage.Height - p.Y;
    p.X = image.Width - warterImage.Width - p.X;
    break;
    }
    newGp.DrawImage(warterImage, new Rectangle(p, new Size(warterImage.Width, warterImage.Height)));
    }
    }
    }
    public static void Makewater(Image image, String waterStr, Font font, Brush brush, Point p)
    {
    ImageDeal.Makewater(image, waterStr, font, brush, p, ImagePosition.TopLeft);
    }
    public static void Makewater(Image image, String waterStr, Font font, Brush brush, Point p, ImagePosition imagePosition)
    {
    using (Graphics newGp = Graphics.FromImage(image))
    {
    Int32 stringWidth;
    Int32 stringHeight;
    stringHeight = (int) font.Size;
    stringWidth = (int) (((float) StringDeal.GetBitLength(waterStr) / (float) 2) * (font.Size + 1));
    newGp.CompositingQuality = CompositingQuality.HighQuality;
    //设置高质量插值法
                    newGp.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
    //设置高质量,低速度呈现平滑程度
                    newGp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    //文字抗锯齿
                    newGp.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
    switch (imagePosition)
    {
    case ImagePosition.BottomLeft:
    p.Y = image.Height - stringHeight - p.Y;
    break;
    case ImagePosition.TopRigth:
    p.X = image.Width - stringWidth - p.X;
    break;
    case ImagePosition.BottomRight:
    p.Y = image.Height - stringHeight - p.Y;
    p.X = image.Width - stringWidth - p.X;
    break;
    }
    newGp.DrawString(waterStr, font, brush, p);
    }
    }
    /// <summary>
            /// 高质量保存
            ///http://www.cnblogs.com/pooeo/
            /// </summary>
            /// <param name="image"></param>
            /// <param name="path"></param>
            public static void SaveQuality(Image image, String path)
    {
    ImageCodecInfo myImageCodecInfo;
    Encoder myEncoder;
    EncoderParameter myEncoderParameter;
    EncoderParameters myEncoderParameters;
    myImageCodecInfo = ImageCodecInfo.GetImageEncoders()[0];
    myEncoder = Encoder.Quality;
    myEncoderParameters = new EncoderParameters(1);
    myEncoderParameter = new EncoderParameter(myEncoder, 100L); // 0-100
                myEncoderParameters.Param[0] = myEncoderParameter;
    try
    {
    image.Save(path, myImageCodecInfo, myEncoderParameters);
    }
    finally
    {
    myEncoderParameter.Dispose();
    myEncoderParameters.Dispose();
    }
    }
    }
    //当字符串中有中文时,一个中文的长度表示为2
        //如 StringDeal.GetBitLength("123")没有中文返回的长度是3
        //如 StringDeal.GetBigLength("123四")有中文返回的长度是5,如果直接用"123四".Length返回的是4
        public static class StringDeal
    {
    public static int GetBitLength(string waterStr)
    {
    Regex regex = new Regex("["u4e00-"u9fa5]+");
    int length = waterStr.Length;
    for (int i = 0; i < waterStr.Length; i++)
    {
    if (regex.IsMatch(waterStr.Substring(i, 1)))
    {
    length++;
    }
    }
    return length;
    }
    }
    public enum StringPosition
    {
    TopLeft,
    BottomLeft
    }
    public enum ImagePosition
    {
    TopLeft,
    BottomLeft,
    BottomRight,
    TopRigth
    }
    }
  • 相关阅读:
    MyEclipse配置Tomcat 并编写第一个JSP程序
    ubuntu安装postgresql与postgis
    ubuntu12.10升级至14.04
    ubuntu 12.10无法用apt-get安装软件 Err http://us.archive.ubuntu.com quantal-updates/main Sources 404 Not
    hive0.13网络接口安装
    hive报错 Another instance of Derby may have already booted the database
    前端开发者进阶之函数柯里化Currying
    js中的事件委托
    while 和 for 对比
    小图标文字对齐的终极解决方案
  • 原文地址:https://www.cnblogs.com/lzhdim/p/1368863.html
Copyright © 2020-2023  润新知