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

各种各式各样的图片缩略水印静态,注释写的不多

复制  保存

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
}
}
时间: 2024-10-13 22:21:03

各式各样的图片缩略水印静态类的相关文章

java代码-java实现图片浏览器,要求带图片缩略

问题描述 java实现图片浏览器,要求带图片缩略 其他基本功能已实现,现在就差缩略没写,功能大概是在读入文件时访问所有图片文件,并生成缩略图,在主面板一侧以列表的形式显示,当点击列表中的缩略图时,在主面板中显示原图 解决方案 简单图片浏览器的实现java swing实现简单的图片浏览器缩略图片

java实现图片浏览器,要求带图片缩略

问题描述 java实现图片浏览器,要求带图片缩略 其他基本功能已实现,现在就差缩略没写,功能大概是在读入文件时访问所有图片文件,并生成缩略图,在主面板一侧以列表的形式显示,当点击列表中的缩略图时,在主面板中显示原图 解决方案 简单图片浏览器的实现java swing实现简单的图片浏览器缩略图片 解决方案二: 简单图片浏览器的实现java swing实现简单的图片浏览器缩略图片 解决方案三: 楼主问题不是很清晰,针对你的问题,我有如下建议: 1. 如果使用java生成缩略图,有很多方法,java

asp.net图片缩略和加水印程序

 代码如下 复制代码 ///<summary> /// 获取一个图片按等比例缩小后的大小. ///</summary> ///<param name="maxWidth">需要缩小到的宽度</param> ///<param name="maxHeight">需要缩小到的高度</param> ///<param name="imageOriginalWidth">

Jquery动态进行图片缩略的原理及实现_jquery

复制代码 代码如下: //页面加载完执行resizeImage()函数 $(document).ready(resizeImage()); function resizeImage(){ $(".pic a img").each(function(){ //加载图片至内存,完成后执行 $(this).load(function(){ //获得原始图片高宽 var imgWidth = $(this).width(); var imgHeight = $(this).height();

为何图片经过OSS缩略之后尺寸变大了?——影响不同格式图片文件大小的一些因素和实际示例

简介 OSS提供了基本的图片处理功能和图片格式之间的转换功能,在实际使用过程中,很多用户使用OSS将原图缩略之后输出,在这个过程中也出现了很多用户询问为何缩略之后图片尺寸变大,如这个例子: 原图:http://batchtest.oss-cn-hangzhou.aliyuncs.com/example1.png 处理之后:http://batchtest.oss-cn-hangzhou.aliyuncs.com/example1.png?x-oss-process=image/resize,w_

php 图片处理类,缩略,水印

php 图片处理类,缩略,水印 class Image {  /**   * @var string $fileName 文件名   * @access private   */  private $fileName = '';    /**   * @var gd resource $imageResource 原图像   * @access private   */  private $imageResource = NULL;    /**   * @var int $imageWidth

javascript实现图片等比例缩略效果

,如下:  代码如下 复制代码 <img src="liuhui.jpg" id="img">   <script type="text/javascript"> window.onload = function(){     var size = 600;     var obj = document.getElementById('img');     if (obj.width > obj .height) {

拿起铅笔:创作缩略草图

在Web UI之前,印刷设计师们使用一种称之为缩略草图(thumbnail sketching)的方法来尝试表达及完善创意.这种方法在现在的PC工具时代似乎已经失传. 这里我们将简要介绍什么是缩略草图,以及一些技巧来帮助把这门技术应用到你的工作之中. 在画线框图之前,先草图 线框图(wireframing)的话题由来已久.不需要回避这个话题,许多设计师以及UI/UX专家们认为线框图是一个最基本以及整个项目中第一个步骤,让我们能专注在基础的架构上而不是多余的设计风格上的干扰. 另一种定义说的是线框

android launcher-launcher获取屏幕缩略(预览)图

问题描述 launcher获取屏幕缩略(预览)图 各位大侠!小弟最近修改android launcher 源码,完成一个功能需求是要当长按workspace的空白处的时候出现当前五个屏幕的缩略图,还能滑动!谢谢!小弟才入行不久求指点! 解决方案 主要问题是获取workspace的缩略图.view有这样一个函数 view.draw(Canvas) 使用这个函数就能够拿到view的bitmap图片. 大致代码如下: Bitmap bitmap = Bitmap.createBitmap(w, h,