using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing.Imaging;
using System.Drawing;
using System.IO;
/// <summary>
///Method 的摘要说明
/// </summary>
public class Method
{
public Method()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 添加图片水印
/// </summary>
/// <param name="image1">/此处添加添加被加水印的图片</param>
/// <param name="image2">此外添加添加水印图片</param>
/// <param name="num"></param>
/// <param name="savepath"></param>
private void DrawImage(string image1,string image2,int num,string savepath)
{
System.Drawing.Image image = System.Drawing.Image.FromFile(image1); //此处添加添加被加水印的图片
System.Drawing.Image copyImage = System.Drawing.Image.FromFile(image2); //此外添加添加水印图片
ImageAttributes attributes = new ImageAttributes();
float alpha = num / 255.0f; //这里来设置透明度(只修改此处就可以了)
float[][] colorMatrixElements = {
new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, alpha, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
};
ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);
///设置颜色调整矩阵
attributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
Graphics g = Graphics.FromImage(image);
g.DrawImage(copyImage, new Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel, attributes); //合成图片
g.Dispose(); //销毁对象
image.Save(savepath); //保存合成的图片
}
/// <summary>
/// 图片剪裁
/// </summary>
/// <param name="imagepth">图片路径</param>
/// <param name="savepath">保存路径</param>
/// <param name="x">起始的X坐标</param>
/// <param name="y">起始的Y坐标</param>
/// <param name="newwidth">新图片的宽度</param>
/// <param name="newheith">新图片的高度</param>
protected void CutPic(string imagepth, string savepath, int x, int y, int newwidth, int newheith)
{
//int x = 0, y = 0; //剪裁的照片(起始坐标)
//int newwidth = 2; //图片的宽度(新图片)
//int newheith = 50; //图片的高度(新图片)
//以上是设置的参数
///以下为执行剪裁图片的操作Server.MapPath("UploadImage") + "/main_01.gif"
System.Drawing.Image newImage = System.Drawing.Image.FromFile(imagepth);
///加载
Bitmap b = new Bitmap(newwidth, newheith);
Graphics g = Graphics.FromImage(b);
GraphicsUnit units = GraphicsUnit.Pixel;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
g.DrawImage(newImage, new Rectangle(0, 0, b.Width, b.Height), x, y, newwidth, newheith, units);
///保存剪裁后的图片Server.MapPath("UploadImage") + "/cut.jpg"
b.Save(savepath);
}
/// <summary>
/// 添加文字水印
/// </summary>
/// <param name="wordstring">水印文字</param>
/// <param name="savepath">保存路径</param>
/// <param name="inputstream">FileUpload.PostedFile.InputStream</param>
private void DrawWord(string wordstring,string savepath,Stream inputstream)
{
//FileUpload2.PostedFile.InputStream
System.Drawing.Image img = Bitmap.FromStream(inputstream); //在Cs后台代码中可以直接写 FileUpload2.PostedFile.InputStream
img = img.GetThumbnailImage(img.Size.Width / 1, img.Size.Height / 1, null, System.IntPtr.Zero);
Bitmap output = new Bitmap(img);
Graphics g = Graphics.FromImage(output);
g.DrawString(wordstring, new Font("Courier New", 20), new SolidBrush(Color.Gray), 0, img.Size.Height - 30);
//Server.MapPath("UploadImage") + "/text.jpg"
output.Save(savepath);
}
/// <summary>
/// 压缩图片
/// </summary>
/// <param name="InputStream">FileUpload.PostedFile.InputStream</param>
/// <param name="Ziprate">压缩率</param>
/// <param name="savepath">保存路径</param>
private void Zip(Stream InputStream,int Ziprate,string savepath)
{
//int rate = Convert.ToInt32(DropDownList1.SelectedValue);
//FileUpload1.PostedFile.InputStream
System.Drawing.Image img = Bitmap.FromStream(InputStream);
img = img.GetThumbnailImage(img.Size.Width / Ziprate, img.Size.Height / Ziprate, null, System.IntPtr.Zero);
Graphics g = Graphics.FromImage(img); //定一个graphics实例,参数为要改变分辨率的图片
// g.DrawImage(img, 0, 0, img.Size.Width / rate, img.Size.Height / rate);//将要改变分辨率的图片画到新的位图变量里
//Server.MapPath("UploadImage") + "/text.jpg"
img.Save(savepath);
}
///返回文件类型
/// FileUpload4.PostedFile.ContentType.ToString().Substring(FileUpload4.PostedFile.ContentType.ToString().LastIndexOf("/")+1)
}