您的位置:首页 > 编程语言 > C#

C#实现带指定文本水印的截图(指定窗口与 全屏)

2008-07-21 10:53 519 查看
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
//
using System.ComponentModel;
using System.Drawing.Drawing2D;

namespace CNEE.Framework.VR
{

/// <summary>
/// 提供全屏和指定窗口的截图 以及保存为文件的类
/// </summary>
public class ScreenCaptures
{
public ScreenCaptures()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 全屏截图
/// </summary>
/// <returns></returns>
private static Image CaptureScreen()
{
return CaptureWindow( User32.GetDesktopWindow() );
}
/// <summary>
/// 指定窗口截图
/// </summary>
/// <param name="handle">窗口句柄. (在windows应用程序中, 从Handle属性获得)</param>
/// <returns></returns>
private static Image CaptureWindow(IntPtr handle)
{
IntPtr hdcSrc = User32.GetWindowDC(handle);
User32.RECT windowRect = new User32.RECT();
User32.GetWindowRect(handle,ref windowRect);
int width = windowRect.right - windowRect.left;
int height = windowRect.bottom - windowRect.top;
IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc,width,height);
IntPtr hOld = GDI32.SelectObject(hdcDest,hBitmap);
GDI32.BitBlt(hdcDest,0,0,width,height,hdcSrc,0,0,GDI32.SRCCOPY);
GDI32.SelectObject(hdcDest,hOld);
GDI32.DeleteDC(hdcDest);
User32.ReleaseDC(handle,hdcSrc);
Image img = Image.FromHbitmap(hBitmap);
GDI32.DeleteObject(hBitmap);
return img;
}
/// <summary>
/// 文本水印,string MapPath改成Image img即可实现截图时绘制阴影文本,这里是把图片修改,绘制保存到另外一个图片文件中
/// </summary>
/// <param name="MapPath"></param>
public static void shuiyin(string MapPath,string _watermarkText)
{
//string MapPath="shuiyin/shuiyintu.jpg";
System.Drawing.Bitmap image = new System.Drawing.Bitmap(@"ImageSave/"+MapPath);
Graphics fg = Graphics.FromImage(image);
//
//System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0,0,image.Width,image.Height),Color.Red,Color.Red,1.2f,true);
//
Font font1 = new Font("宋体",22,FontStyle.Bold | FontStyle.Italic );
//g.DrawString("水印",font1,brush,1,1);
// using (Brush brush2 = new SolidBrush(Color.FromArgb(128, Color.Red)))
//{
// fg.DrawString(_watermarkText, font1, brush2, 1, image.Height/2);
//}
//Graphics picture=Graphics.FromImage(image);
// 确定水印文字的字体大小
int[] sizes = new int[]{32, 30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4};
Font crFont = null;
SizeF crSize = new SizeF();
for (int i = 0;i < sizes.Length; i++)
{
crFont = new Font("Arial Black", sizes[i], FontStyle.Bold);
crSize = fg.MeasureString(_watermarkText, crFont);//picture

if((ushort)crSize.Width < (ushort)image.Width)
{
break;
}
}
PointF pt=new PointF(0,0);
// 画阴影文字
Brush TransparentBrush0 = new SolidBrush(Color.FromArgb(255, Color.Black));
Brush TransparentBrush1 = new SolidBrush(Color.FromArgb(255, Color.Black));
fg.DrawString(_watermarkText,crFont,TransparentBrush0, pt.X, pt.Y + 1);
fg.DrawString(_watermarkText,crFont,TransparentBrush0, pt.X + 1, pt.Y);

fg.DrawString(_watermarkText,crFont,TransparentBrush1, pt.X + 1, pt.Y + 1);
fg.DrawString(_watermarkText,crFont,TransparentBrush1, pt.X, pt.Y + 2);
fg.DrawString(_watermarkText,crFont,TransparentBrush1, pt.X + 2, pt.Y);

TransparentBrush0.Dispose();
TransparentBrush1.Dispose();
fg.Save();
//if(!Directory.Exists("ImageSave//ImageSave"))
//Directory.CreateDirectory("ImageSave//ImageSave");

image.Save(MapPath);
image.Dispose();
//
fg.Dispose();
font1.Dispose();
crFont.Dispose();
//if(File.Exists(MapPath))
//File.Delete(MapPath);
//font1.Dispose();
//g.Dispose();
//image.Dispose();

}
/// <summary>
/// 指定窗口截图 保存为图片文件
/// </summary>
/// <param name="handle"></param>
/// <param name="filename"></param>
/// <param name="format"></param>
public static void CaptureWindowToFile(IntPtr handle, string filename, ImageFormat format)
{
if(!Directory.Exists("ImageSave"))
Directory.CreateDirectory("ImageSave");
Image img = CaptureWindow(handle);
//

Graphics g = Graphics.FromImage(img);
//
//System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0,0,image.Width,image.Height),Color.Red,Color.Red,1.2f,true);
//
Font font1 = new Font("黑体",24,FontStyle.Bold );
//g.DrawString("水印",font1,brush,1,1);
using (Brush brush2 = new SolidBrush(Color.FromArgb(128, Color.WhiteSmoke)))
{
g.DrawString("Author:Rains(QQ:233685340)", font1, brush2, 1, img.Height/2);
}
g.Save();
if(!Directory.Exists("ImageSave//ImageSave"))
Directory.CreateDirectory("ImageSave//ImageSave");
//
img.Save(@"ImageSave/ImageSave/"+filename,format);
img.Dispose();
}
/// <summary>
/// 全屏截图 保存为文件
/// </summary>
/// <param name="filename"></param>
/// <param name="format"></param>
public static void CaptureScreenToFile(string filename, ImageFormat format)
{
if(!Directory.Exists("ImageSave"))
Directory.CreateDirectory("ImageSave");
Image img = CaptureScreen();
//
//System.Drawing.Bitmap image = new System.Drawing.Bitmap(img);
Graphics g = Graphics.FromImage(img);
//
//System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0,0,image.Width,image.Height),Color.Red,Color.Red,1.2f,true);
//
Font font1 = new Font("华文彩云",24,FontStyle.Bold | FontStyle.Italic );
//g.DrawString("水印",font1,brush,1,1);
using (Brush brush2 = new SolidBrush(Color.FromArgb(128, Color.Red)))
{
g.DrawString("Author:Rains--QQ:233685340", font1, brush2, 1, img.Height/2);
}
g.Save();
if(!Directory.Exists("ImageSave"))
Directory.CreateDirectory("ImageSave");

//image.Save(@"ImageSave/"+MapPath);
//image.Dispose();
//
img.Save(@"ImageSave/"+filename,format);
img.Dispose();

}

/// <summary>
/// 辅助类 定义 Gdi32 API 函数
/// </summary>
private class GDI32
{

public const int SRCCOPY = 0x00CC0020;
[DllImport("gdi32.dll")]
public static extern bool BitBlt(IntPtr hObject,int nXDest,int nYDest,
int nWidth,int nHeight,IntPtr hObjectSource,
int nXSrc,int nYSrc,int dwRop);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC,int nWidth,
int nHeight);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
[DllImport("gdi32.dll")]
public static extern bool DeleteDC(IntPtr hDC);
[DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
[DllImport("gdi32.dll")]
public static extern IntPtr SelectObject(IntPtr hDC,IntPtr hObject);
}

/// <summary>
/// 辅助类 定义User32 API函数
/// </summary>
private class User32
{
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern IntPtr ReleaseDC(IntPtr hWnd,IntPtr hDC);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowRect(IntPtr hWnd,ref RECT rect);
}
}
}
//
// //-----------------------------------------------------------------------------
// //
// //-----------------------------------------------------------------------------
// /// <summary>
// /// 给一个位图绘制水印文字
// /// </summary>
// /// <param name="text">水印文本</param>
// /// <param name="x">起始点</param>
// /// <param name="y">起始点</param>
// /// <param name="opacity">不透明度,0~1</param>
// private Bitmap DrawWatermark(Image image,string text, Font font,Brush brush,int x,int y,double opacity)
// {
// Bitmap bm1 = new Bitmap(image);
// Graphics g1=Graphics.FromImage(bm1);
// //测量水印文字的大小,然后申请一个新的位图
// SizeF sizef=g1.MeasureString(text,font);
// Bitmap bm2=new Bitmap((int)sizef.Width,(int)sizef.Height);
// Graphics g2=Graphics.FromImage(bm2);
// g2.DrawImage(bm1, 0,0,new Rectangle(x, y, bm2.Width, bm2.Height),GraphicsUnit.Pixel);
// g2.DrawString(text,font,brush,0,0);
// BitmapData data1=bm1.LockBits(new Rectangle(0,0,bm1.Width,bm1.Height),ImageLockMode.ReadWrite,PixelFormat.Format24bppRgb);
// BitmapData data2=bm2.LockBits(new Rectangle(0,0,bm2.Width,bm2.Height),ImageLockMode.ReadWrite,PixelFormat.Format24bppRgb);
//// unsafe
//// {
//// byte* p1=(byte*)(void*)data1.Scan0;
//// byte* p2=(byte*)(void*)data2.Scan0;
//// for(int j=0;j<bm2.Height;j++)
//// {
//// for(int i=0;i<bm2.Width;i++)
//// {
//// p1[(y+j) * data1.Stride+ (x+i) *3]=(byte)(p1[(y+j)*data1.Stride+(x+i)*3]*(1-opacity)+opacity*p2[j*data2.Stride+i*3]);
//// p1[(y+j) * data1.Stride + (x+i) * 3 + 1] = (byte)(p1[(y+j) * data1.Stride + (x+i) * 3 + 1] * (1-opacity) + opacity * p2[j * data2.Stride + i * 3+1]);
//// p1[(y+j) * data1.Stride + (x+i) * 3 + 2] = (byte)(p1[(y+j) * data1.Stride + (x+i) * 3 + 2] * (1-opacity) + opacity* p2[j * data2.Stride + i * 3+2]);
//// }
//// }
// bm1.UnlockBits(data1);
// bm2.UnlockBits(data2);
//// }
// return bm1;
// }
// }
// //图片印章类
// //
// /// <summary>
// /// StamperImage给图片加上印章。
// /// </summary>
// public class StamperImage: IDisposable
//
// {
// public enum Position : int
// {
// TopLeft = 1,
// TopRight = 2,
// BottomLeft = 3,
// BottomRight = 4,
// Center = 5
// }
//
// int _margin = 3;
// private Position _stampPos = Position.TopLeft;
// private string _originImageFile = "",_stampImageFile = "";
// private Bitmap _bmOriginImage,_bmStampImage;
// private string _stampText = "";
// private bool _textStampMode = false;
// private Font _textFont = new Font("Arial,宋体",10);
// private Color _textColor = Color.White;
// private bool _dispose = false;
//
// public StamperImage()
// {
// _dispose = true;
// }
// public StamperImage(string sOriginImageFile,string sStampImageFile)
// {
// _originImageFile = sOriginImageFile;
// _stampImageFile = sStampImageFile;
// try
// {
// _bmOriginImage = new Bitmap(_originImageFile);
// _bmStampImage = new Bitmap(_stampImageFile);
// }
// catch(Exception e)
// {
// throw e;
// }
// }
// public StamperImage(Bitmap sOrginImage,Bitmap sStampImage)
// {
// _bmOriginImage = sOrginImage;
// _bmStampImage = sStampImage;
// }
// public StamperImage(string sOriginImageFile,string sStampText,Font sStampTextFont,Color sStampTextColor)
// {
// _originImageFile = sOriginImageFile;
// try
// {
// _bmOriginImage = new Bitmap(_originImageFile);
// }
// catch(Exception e)
// {
// throw e;
// }
// _textStampMode = true;
// _stampText = sStampText;
// _textFont = sStampTextFont;
// _textColor = sStampTextColor;
// }
// public StamperImage(string sOriginImageFile,string sStampText,Font sStampTextFont)
// {
// _originImageFile = sOriginImageFile;
// try
// {
// _bmOriginImage = new Bitmap(_originImageFile);
// }
// catch(Exception e)
// {
// throw e;
// }
// _textStampMode = true;
// _stampText = sStampText;
// _textFont = sStampTextFont;
// }
// public StamperImage(string sOriginImageFile,string sStampText,Color sStampTextColor)
// {
// _originImageFile = sOriginImageFile;
// try
// {
// _bmOriginImage = new Bitmap(_originImageFile);
// }
// catch(Exception e)
// {
// throw e;
// }
// _textStampMode = true;
// _stampText = sStampText;
// _textColor = sStampTextColor;
// }
//
// private void Dispose(bool disposing)
// {
// if(!_dispose)
// {
// if(disposing)
// {
// if(_bmOriginImage!=null)_bmOriginImage.Dispose();
// if(_bmStampImage!=null)_bmStampImage.Dispose();
// _dispose = true;
// }
// }
// }
//
// public void Dispose()
// {
// Dispose(true);
// GC.SuppressFinalize(this);
// }
// /// <summary>
// /// 给图片加上印章
// /// </summary>
// public void Stamper()
// {
// int _iOImageWith,_iOImageHeight;
// Point _stampPoint;
// try
// {
// Graphics _graSettingImage = Graphics.FromImage(_bmOriginImage);
// _iOImageWith = _bmOriginImage.Width;
// _iOImageHeight = _bmOriginImage.Height;
// if(_textStampMode)
// {
// int _iFontHeight = _textFont.Height;
// int _iFontWith = ((int)_textFont.SizeInPoints * _stampText.Length);
// switch(_stampPos)
// {
// case Position.TopLeft:_stampPoint = new Point(_margin,_margin);break;
// case Position.TopRight:_stampPoint = new Point(_iOImageWith - _margin - _iFontWith,_margin);break;
// case Position.BottomLeft:_stampPoint = new Point(_margin,_iOImageHeight - _iFontHeight - _margin);break;
// case Position.BottomRight:_stampPoint = new Point(_iOImageWith - _margin - _iFontWith,_iOImageHeight - _iFontHeight - _margin);break;
// case Position.Center:_stampPoint = new Point(_iOImageWith/2 - _iFontWith/2,_iOImageHeight/2 - _iFontHeight/2);break;
// default:_stampPoint = new Point(_margin,_margin);break;
// }
// _graSettingImage.DrawString(_stampText,_textFont,new SolidBrush(_textColor),_stampPoint);
// }
// else
// {
// int _iSImageWith = _bmStampImage.Width;
// int _iSImageHeight = _bmStampImage.Height;
// switch(_stampPos)
// {
// case Position.TopLeft:_stampPoint = new Point(_margin,_margin);break;
// case Position.TopRight:_stampPoint = new Point(_iOImageWith - _margin - _iSImageWith,_margin);break;
// case Position.BottomLeft:_stampPoint = new Point(_margin,_iOImageHeight - _iSImageHeight - _margin);break;
// case Position.BottomRight:_stampPoint = new Point(_iOImageWith - _margin - _iSImageWith,_iOImageHeight - _iSImageHeight - _margin);break;
// case Position.Center:_stampPoint = new Point(_iOImageWith/2 - _iSImageWith/2,_iOImageHeight/2 - _iSImageHeight/2);break;
// default:_stampPoint = new Point(_margin,_margin);break;
// }
// _graSettingImage.DrawImage(_bmStampImage,_stampPoint);
// }
// _graSettingImage.Dispose();
// }
// catch(Exception e)
// {
// throw e;
// }
// }
// public void Save(string _targetImageFile)
// {
// _bmOriginImage.Save(_targetImageFile);
// }
// public void Save(System.IO.Stream _stream)
// {
// _bmOriginImage.Save(_stream,ImageFormat.Jpeg);
// }
//
//
// /// <summary>
// /// 印章的边距离
// /// </summary>
// public int Margin
// {
// get
// {
// return _margin;
// }
// set
// {
// _margin = value;
// }
// }
// /// <summary>
// /// 印章的位置
// /// </summary>
// public Position StampPos
// {
// get
// {
// return _stampPos;
// }
// set
// {
// _stampPos = value;
// }
// }
//
// /// <summary>
// /// 需要加上印章的图像文件
// /// </summary>
// public string OriginImageFile
// {
// get
// {
// return _originImageFile;
// }
// set
// {
// try
// {
// _originImageFile = value;
// _bmOriginImage = new Bitmap(_originImageFile);
// _dispose = false;
// }
// catch(Exception e)
// {
// throw e;
// }
// }
// }
// /// <summary>
// /// 需要加上印章的图像
// /// </summary>
// public Bitmap OriginImage
// {
// get
// {
// return _bmOriginImage;
// }
// set
// {
// _bmOriginImage = value;
// _dispose = false;
// }
// }
// /// <summary>
// /// 印章图像文件路径
// /// </summary>
// public string StampImageFile
// {
// get
// {
// return _stampImageFile;
// }
// set
// {
// try
// {
// _stampImageFile = value;
// _bmStampImage = new Bitmap(_stampImageFile);
// _dispose = false;
// }
// catch(Exception e)
// {
// throw e;
// }
//
// }
// }
// /// <summary>
// /// 印章图像
// /// </summary>
// public Bitmap StampImage
// {
// get
// {
// return _bmStampImage;
// }
// set
// {
// _bmStampImage = value;
// _dispose = false;
// }
// }
// /// <summary>
// /// 印章文字
// /// </summary>
// public string StampText
// {
// get
// {
// return _stampText;
// }
// set
// {
// _stampText = value;
// }
// }
// /// <summary>
// /// 印章文字的字体
// /// </summary>
// public Font TextFont
// {
// get
// {
// return _textFont;
// }
// set
// {
// _textFont = value;
// }
// }
// /// <summary>
// /// 印章文字的颜色
// /// </summary>
// public Color TextColor
// {
// get
// {
// return _textColor;
// }
// set
// {
// _textColor = value;
// }
// }
// /// <summary>
// /// 印章模式,true加上text印章,否则为图像印章
// /// </summary>
// public bool TextStampMode
// {
// get
// {
// return _textStampMode;
// }
// set
// {
// _textStampMode = value;
// }
// }
// }
// //-------------------------------------------------------------------------------
// //指定图片加水印
// //-------------------------------------------------------------------------------
//
// /// <summary>
// /// 水印的类型
// /// </summary>
// public enum WaterMarkType
// {
// /// <summary>
// /// 文字水印
// /// </summary>
// TextMark,
// /// <summary>
// /// 图片水印
// /// </summary>
// //ImageMark // 暂时只能添加文字水印
// };
//
// /// <summary>
// /// 水印的位置
// /// </summary>
// public enum WaterMarkPosition
// {
// /// <summary>
// /// 左上角
// /// </summary>
// WMP_Left_Top,
// /// <summary>
// /// 左下角
// /// </summary>
// WMP_Left_Bottom,
// /// <summary>
// /// 右上角
// /// </summary>
// WMP_Right_Top,
// /// <summary>
// /// 右下角
// /// </summary>
// WMP_Right_Bottom
// };
//
// /// <summary>
// /// 处理图片的类(包括加水印,生成缩略图)
// /// </summary>
// public class ImageWaterMark
// {
// public ImageWaterMark()
// {
// //
// // TODO: 在此处添加构造函数逻辑
// //
// }
//
// #region 给图片加水印
// /// <summary>
// /// 添加水印(分图片水印与文字水印两种)
// /// </summary>
// /// <param name="oldpath">原图片绝对地址</param>
// /// <param name="newpath">新图片放置的绝对地址</param>
// /// <param name="wmtType">要添加的水印的类型</param>
// /// <param name="sWaterMarkContent">水印内容,若添加文字水印,此即为要添加的文字;
// /// 若要添加图片水印,此为图片的路径</param>
// public void addWaterMark(string oldpath, string newpath,
// WaterMarkType wmtType, string sWaterMarkContent)
// {
// try
// {
// Image image = Image.FromFile(oldpath);
//
// Bitmap b = new Bitmap(image.Width, image.Height,
// PixelFormat.Format24bppRgb);
//
// Graphics g = Graphics.FromImage(b);
// g.Clear(Color.White);
// g.SmoothingMode = SmoothingMode.HighQuality;
// g.InterpolationMode = InterpolationMode.High;
//
// g.DrawImage(image, 0, 0, image.Width, image.Height);
//
// switch (wmtType)
// {
// /*case WaterMarkType.ImageMark:
// //图片水印
// this.addWatermarkImage(g,
// Page.Server.MapPath(Watermarkimgpath),
// WatermarkPosition,image.Width,image.Height);
// break;*/
// case WaterMarkType.TextMark:
// //文字水印
// this.addWatermarkText(g, sWaterMarkContent, "WM_BOTTOM_RIGHT",
// image.Width, image.Height);
// break;
// }
//
// b.Save(newpath);
// b.Dispose();
// image.Dispose();
// }
// catch
// {
// if(File.Exists(oldpath))
// {
// File.Delete(oldpath);
// }
// }
// finally
// {
// if(File.Exists(oldpath))
// {
// File.Delete(oldpath);
// }
// }
// }
//
// /// <summary>
// /// 加水印文字
// /// </summary>
// /// <param name="picture">imge 对象</param>
// /// <param name="_watermarkText">水印文字内容</param>
// /// <param name="_watermarkPosition">水印位置</param>
// /// <param name="_width">被加水印图片的宽</param>
// /// <param name="_height">被加水印图片的高</param>
// private void addWatermarkText(Graphics picture, string _watermarkText,
// string _watermarkPosition, int _width, int _height)
// {
// // 确定水印文字的字体大小
// int[] sizes = new int[]{32, 30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4};
// Font crFont = null;
// SizeF crSize = new SizeF();
// for (int i = 0;i < sizes.Length; i++)
// {
// crFont = new Font("Arial Black", sizes[i], FontStyle.Bold);
// crSize = picture.MeasureString(_watermarkText, crFont);
//
// if((ushort)crSize.Width < (ushort)_width)
// {
// break;
// }
// }
// PointF pt=new PointF(0,0);
//
// // 生成水印图片(将文字写到图片中)
// Bitmap floatBmp = new Bitmap((int)crSize.Width + 3,
// (int)crSize.Height + 3, PixelFormat.Format32bppArgb);
// Graphics fg=Graphics.FromImage(floatBmp);
//
//
// // 画阴影文字
// Brush TransparentBrush0 = new SolidBrush(Color.FromArgb(255, Color.Black));
// Brush TransparentBrush1 = new SolidBrush(Color.FromArgb(255, Color.Black));
// fg.DrawString(_watermarkText,crFont,TransparentBrush0, pt.X, pt.Y + 1);
// fg.DrawString(_watermarkText,crFont,TransparentBrush0, pt.X + 1, pt.Y);
//
// fg.DrawString(_watermarkText,crFont,TransparentBrush1, pt.X + 1, pt.Y + 1);
// fg.DrawString(_watermarkText,crFont,TransparentBrush1, pt.X, pt.Y + 2);
// fg.DrawString(_watermarkText,crFont,TransparentBrush1, pt.X + 2, pt.Y);
//
// TransparentBrush0.Dispose();
// TransparentBrush1.Dispose();
//
// // 画文字
// fg.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.HighQuality;
// fg.DrawString(_watermarkText,
// crFont, new SolidBrush(Color.White),
// pt.X, pt.Y, StringFormat.GenericDefault);
//
// // 保存刚才的操作
// fg.Save();
// fg.Dispose();
// // floatBmp.Save("d://WebSite//DIGITALKM//ttt.jpg");
//
// // 将水印图片加到原图中
// this.addWatermarkImage(
// picture,
// new Bitmap(floatBmp),
// "WM_BOTTOM_RIGHT",
// _width,
// _height);
// }
//
// /// <summary>
// /// 加水印图片
// /// </summary>
// /// <param name="picture">imge 对象</param>
// /// <param name="iTheImage">Image对象(以此图片为水印)</param>
// /// <param name="_watermarkPosition">水印位置</param>
// /// <param name="_width">被加水印图片的宽</param>
// /// <param name="_height">被加水印图片的高</param>
// private void addWatermarkImage( Graphics picture,Image iTheImage,
// string _watermarkPosition,int _width,int _height)
// {
// Image watermark = new Bitmap(iTheImage);
//
// ImageAttributes imageAttributes = new ImageAttributes();
// ColorMap colorMap = new ColorMap();
//
// colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
// colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
// ColorMap[] remapTable = {colorMap};
//
// imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
//
// 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, 0.3f, 0.0f},
// new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
// };
//
// ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);
//
// imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
//
// int xpos = 0;
// int ypos = 0;
// int WatermarkWidth = 0;
// int WatermarkHeight = 0;
// double bl = 1d;
//
// //计算水印图片的比率
// //取背景的1/4宽度来比较
// if ((_width > watermark.Width * 4) && (_height > watermark.Height * 4))
// {
// bl = 1;
// }
// else if ((_width > watermark.Width * 4) && (_height<watermark.Height * 4))
// {
// bl = Convert.ToDouble(_height / 4) / Convert.ToDouble(watermark.Height);
//
// }
// else if ((_width < watermark.Width * 4) && (_height > watermark.Height * 4))
// {
// bl = Convert.ToDouble(_width / 4) / Convert.ToDouble(watermark.Width);
// }
// else
// {
// if ((_width * watermark.Height) > (_height * watermark.Width))
// {
// bl = Convert.ToDouble(_height / 4) / Convert.ToDouble(watermark.Height);
//
// }
// else
// {
// bl = Convert.ToDouble(_width / 4) / Convert.ToDouble(watermark.Width);
//
// }
//
// }
//
// WatermarkWidth = Convert.ToInt32(watermark.Width * bl);
// WatermarkHeight = Convert.ToInt32(watermark.Height * bl);
//
// switch (_watermarkPosition)
// {
// case "WM_TOP_LEFT":
// xpos = 10;
// ypos = 10;
// break;
// case "WM_TOP_RIGHT":
// xpos = _width - WatermarkWidth - 10;
// ypos = 10;
// break;
// case "WM_BOTTOM_RIGHT":
// xpos = _width - WatermarkWidth - 10;
// ypos = _height -WatermarkHeight - 10;
// break;
// case "WM_BOTTOM_LEFT":
// xpos = 10;
// ypos = _height - WatermarkHeight - 10;
// break;
// }
//
// picture.DrawImage(
// watermark,
// new Rectangle(xpos, ypos, WatermarkWidth, WatermarkHeight),
// 0,
// 0,
// watermark.Width,
// watermark.Height,
// GraphicsUnit.Pixel,
// imageAttributes);
//
// watermark.Dispose();
// imageAttributes.Dispose();
// }
//
// /// <summary>
// /// 加水印图片
// /// </summary>
// /// <param name="picture">imge 对象</param>
// /// <param name="WaterMarkPicPath">水印图片的地址</param>
// /// <param name="_watermarkPosition">水印位置</param>
// /// <param name="_width">被加水印图片的宽</param>
// /// <param name="_height">被加水印图片的高</param>
// private void addWatermarkImage( Graphics picture,string WaterMarkPicPath,
// string _watermarkPosition,int _width,int _height)
// {
// Image watermark = new Bitmap(WaterMarkPicPath);
//
// this.addWatermarkImage(picture, watermark, _watermarkPosition, _width,
// _height);
// }
// #endregion
//
// #region 生成缩略图
// /// <summary>
// /// 保存图片
// /// </summary>
// /// <param name="image">Image 对象</param>
// /// <param name="savePath">保存路径</param>
// /// <param name="ici">指定格式的编解码参数</param>
// private void SaveImage(Image image, string savePath, ImageCodecInfo ici)
// {
// //设置 原图片 对象的 EncoderParameters 对象
// EncoderParameters parameters = new EncoderParameters(1);
// parameters.Param[0] = new EncoderParameter(
// System.Drawing.Imaging.Encoder.Quality, ((long) 90));
// image.Save(savePath, ici, parameters);
// parameters.Dispose();
// }
//
// /// <summary>
// /// 获取图像编码解码器的所有相关信息
// /// </summary>
// /// <param name="mimeType">包含编码解码器的多用途网际邮件扩充协议 (MIME) 类型的字符串</param>
// /// <returns>返回图像编码解码器的所有相关信息</returns>
// private ImageCodecInfo GetCodecInfo(string mimeType)
// {
// ImageCodecInfo[] CodecInfo = ImageCodecInfo.GetImageEncoders();
// foreach(ImageCodecInfo ici in CodecInfo)
// {
// if(ici.MimeType == mimeType)
// return ici;
// }
// return null;
// }
//
//// /// <summary>
//// /// 生成缩略图
//// /// </summary>
//// /// <param name="sourceImagePath">原图片路径(相对路径)</param>
//// /// <param name="thumbnailImagePath">生成的缩略图路径,如果为空则保存为原图片路径(相对路径)</param>
//// /// <param name="thumbnailImageWidth">缩略图的宽度(高度与按源图片比例自动生成)</param>
//// public void ToThumbnailImages(
//// string SourceImagePath,
//// string ThumbnailImagePath,
//// int ThumbnailImageWidth)
//// {
//// Hashtable htmimes = new Hashtable();
//// htmimes[".jpeg"] = "image/jpeg";
//// htmimes[".jpg"] = "image/jpeg";
//// htmimes[".png"] = "image/png";
//// htmimes[".tif"] = "image/tiff";
//// htmimes[".tiff"] = "image/tiff";
//// htmimes[".bmp"] = "image/bmp";
//// htmimes[".gif"] = "image/gif";
////
//// // 取得原图片的后缀
//// string sExt = SourceImagePath.Substring(
//// SourceImagePath.LastIndexOf(".")).ToLower();
////
//// //从 原图片 创建 Image 对象
//// Image image = Image.FromFile(SourceImagePath);
//// int num = ((ThumbnailImageWidth / 4) * 3);
//// int width = image.Width;
//// int height = image.Height;
////
//// //计算图片的比例
//// if ((((double) width) / ((double) height)) >= 1.3333333333333333f)
//// {
//// num = ((height * ThumbnailImageWidth) / width);
//// }
//// else
//// {
//// ThumbnailImageWidth = ((width * num) / height);
//// }
//// if ((ThumbnailImageWidth < 1) || (num < 1))
//// {
//// return;
//// }
////
//// //用指定的大小和格式初始化 Bitmap 类的新实例
//// Bitmap bitmap = new Bitmap(ThumbnailImageWidth, num,
//// PixelFormat.Format32bppArgb);
////
//// //从指定的 Image 对象创建新 Graphics 对象
//// Graphics graphics = Graphics.FromImage(bitmap);
////
//// //清除整个绘图面并以透明背景色填充
//// graphics.Clear(Color.Transparent);
////
//// graphics.SmoothingMode = SmoothingMode.HighQuality;
//// graphics.InterpolationMode = InterpolationMode.High;
////
//// //在指定位置并且按指定大小绘制 原图片 对象
//// graphics.DrawImage(image, new Rectangle(0, 0, ThumbnailImageWidth, num));
//// image.Dispose();
////
//// try
//// {
//// //将此 原图片 以指定格式并用指定的编解码参数保存到指定文件
//// SaveImage(bitmap, ThumbnailImagePath,
//// GetCodecInfo((string)htmimes[sExt]));
//// }
//// catch(System.Exception e)
//// {
//// throw e;
//// }
//// }
// #endregion
// }
// public class Hashtable
// {
// public Hashtable()
// {}
// }
// }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: