?? drawmark.cs
字號:
?using System;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
/// <summary>
/// 圖片生成水印效果
/// </summary>
public class DrawMark
{
#region 變量聲明
/// <summary>
/// 表示水印圖片位置的枚舉
/// </summary>
public enum Location
{
/// <summary>
/// 水印位于源圖片左上角
/// </summary>
LeftTop,
/// <summary>
/// 水印位于源圖片左下角
/// </summary>
LeftBottom,
/// <summary>
/// 水印位于源圖片右上角
/// </summary>
RightTop,
/// <summary>
/// 水印位于源圖片右下角
/// </summary>
RightBottom,
/// <summary>
/// 水印位于源圖片中間
/// </summary>
Middle
}
#endregion
#region 獲取圖片
/// <summary>
/// 將水印填充到指定的圖片
/// </summary>
/// <param name="ImagePath">設(shè)置源圖片路徑。</param>
/// <param name="MarkPath">設(shè)置水印圖片路徑。</param>
/// <param name="Locus">設(shè)置一個值,該值表示水印位于源圖片的位置。</param>
/// <returns></returns>
public bool GetImage(string ImagePath, string MarkPath, Location Locus)
{
File.Copy(ImagePath, ImagePath + ".bak");
File.Delete(ImagePath);
Image OriginalImage = Image.FromFile(ImagePath + ".bak");
Image CopyImage = Image.FromFile(MarkPath);
if (OriginalImage.Width > CopyImage.Width && OriginalImage.Height > CopyImage.Height)
{
Graphics g = Graphics.FromImage(OriginalImage);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.HighQuality;
int x, y;
switch (Locus)
{
case Location.LeftTop:
x = 0;
y = 0;
break;
case Location.LeftBottom:
x = 0;
y = OriginalImage.Height - CopyImage.Height;
break;
case Location.RightTop:
x = OriginalImage.Width - CopyImage.Width;
y = 0;
break;
case Location.RightBottom:
x = OriginalImage.Width - CopyImage.Width;
y = OriginalImage.Height - CopyImage.Height;
break;
case Location.Middle:
x = (OriginalImage.Width - CopyImage.Width) / 2;
y = (OriginalImage.Height - CopyImage.Height) / 2;
break;
default:
x = OriginalImage.Width - CopyImage.Width;
y = OriginalImage.Height - CopyImage.Height;
break;
}
g.DrawImage(CopyImage, new Rectangle(x, y, CopyImage.Width, CopyImage.Height), 0, 0, CopyImage.Width, CopyImage.Height, GraphicsUnit.Pixel);
g.Dispose();
OriginalImage.Save(ImagePath);
OriginalImage.Dispose();
CopyImage.Dispose();
File.Delete(ImagePath + ".bak");
return true;
}
else
{
OriginalImage.Dispose();
CopyImage.Dispose();
File.Delete(ImagePath + ".bak");
return false;
}
}
/// <summary>
/// 獲取圖片縮略圖
/// </summary>
/// <param name="ImagePath">設(shè)置源圖片路徑。</param>
/// <param name="width">設(shè)置一個值,該值表示縮略圖的寬度。</param>
public void GetSmallImage(string ImagePath, int width)
{
File.Copy(ImagePath, ImagePath + ".bak");
File.Delete(ImagePath);
Image OriginalImage = Image.FromFile(ImagePath + ".bak");
Image ThumbnailImage = OriginalImage.GetThumbnailImage(width, Convert.ToInt32(width * 0.75), null, IntPtr.Zero);
ThumbnailImage.Save(ImagePath);
OriginalImage.Dispose();
ThumbnailImage.Dispose();
File.Delete(ImagePath + ".bak");
}
#endregion
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -