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

简介: 各种各式各样的图片缩略水印静态,注释写的不多 复制  保存 using System; using System.Collections.Generic; using System.Drawing; using System.
各种各式各样的图片缩略水印静态,注释写的不多
复制   保存
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
}
}
目录
相关文章
|
存储 算法 安全
第4章 数据库安全性——4.5 数据加密
第4章 数据库安全性——4.5 数据加密
|
10月前
|
Web App开发 编解码 vr&ar
使用Web浏览器访问UE应用的最佳实践
在3D/XR应用开发中,尤其是基于UE(虚幻引擎)开发的高精度场景,传统终端因硬件局限难以流畅运行高帧率、复杂效果的三维应用。实时云渲染技术,将渲染任务转移至云端服务器,降低终端硬件要求,确保用户获得流畅体验。具备弹性扩展、优化传输协议、跨平台支持和安全性等优势,适用于多种终端和场景,特别集成像素流送技术,帮助UE开发者实现低代码上云操作,简化部署流程,保留UE引擎的强大开发能力,确保画面精美且终端轻量化。
436 17
使用Web浏览器访问UE应用的最佳实践
|
Scala 开发工具 git
剖析响应式编程的本质
剖析响应式编程的本质
剖析响应式编程的本质
|
存储 弹性计算 小程序
阿里云服务器的四种购买方式及其适合对象和购买流程简介
对于第一次购买云服务器的新手用户来说,阿里云服务器购买页面有点复杂,从云服务器购买、配置选择到网站上线全过程都是很多新手用户关心的问题,2023年了,小编决定写一篇阿里云服务器购买流程的图文教程。
665 1
阿里云服务器的四种购买方式及其适合对象和购买流程简介
|
存储 分布式计算 监控
每年节约数亿元大数据成本,阿里巴巴数据中台成本治理怎么做的?
大数据环境下,数据的存储和计算成本一直居高不下,是每一个企业数字化转型过程中的都会遇到的难题。阿里巴巴作为业内领先的数据智能公司,也遇到过类似的问题,但是凭借着领先的方法论和产品,阿里巴巴每年能够节约数亿元的存储和计算成本。本篇,我们就来聊聊阿里巴巴的资源优化方法论和Dataphin的资源治理和优化能力。
每年节约数亿元大数据成本,阿里巴巴数据中台成本治理怎么做的?
|
SQL 存储 缓存
写着简单跑得又快的数据库语言 SPL
单纯的存储并不是数据库的目标,数据库实现的重要功能有两条:计算、事务!也就是我们常说的OLAP和OLTP,数据库的存储都是为这两件事服务的。
828 0
写着简单跑得又快的数据库语言 SPL
|
机器学习/深度学习 算法
语义分割的几种算法
语义分割的几种算法
376 0
|
机器学习/深度学习 人工智能
AI:2020年6月24日北京智源大会演讲分享之强化学习专题论坛——11: 10-11: 40 秦志伟《深度强化学习在网约车交易市场中的应用 》
AI:2020年6月24日北京智源大会演讲分享之强化学习专题论坛——11: 10-11: 40 秦志伟《深度强化学习在网约车交易市场中的应用 》
AI:2020年6月24日北京智源大会演讲分享之强化学习专题论坛——11: 10-11: 40 秦志伟《深度强化学习在网约车交易市场中的应用 》