|
|
Posted on
2004-12-08 12:48
冷风.net
阅读( 532)
评论()
收藏
举报
//*************************************************************************************************
//完成日期: 2004-11-5
//作者: 何利民
//程序功能: 圖片功能類
//*************************************************************************************************
using System;
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Imaging;
namespace ImagesSpaceName
  {
 定義結構對象#region 定義結構對象
 /**//// <summary>
/// 錯誤信息
/// </summary>
public struct ErrorStruct
 {
public string source; //來源
public string sourcename; //來源名稱
public string content; //錯誤內容
public string text; //錯誤信息
}
#endregion
 定義對齊枚舉對象#region 定義對齊枚舉對象
 /**//// <summary>
/// 水平對齊
/// </summary>
public enum Align
 {
left = 1,
center = 2,
right = 3
}
 /**//// <summary>
/// 垂直對齊
/// </summary>
public enum Valign
 {
top = 1,
middle = 2,
bottom =3
}
 /**//// <summary>
/// 標題對齊
/// </summary>
public enum WaterTitle
 {
top,
bottom,
left,
right,
none
}
#endregion
public class ImagesClass
 {
 定義變量#region 定義變量
protected static string[] ERRORSOURCE = new string[2]; //錯誤來源
protected static string[] ERRORINFO = new string[10]; //錯誤信息
public ErrorStruct ErrorRecord; //記錄錯誤信息
#endregion
 初使化構造函數 ImagesClass()#region 初使化構造函數 ImagesClass()
public ImagesClass()
 {
ERRORSOURCE[0] = "來源屬性";
ERRORSOURCE[1] = "來源方法";
ERRORINFO[0] = "值應為數字";
ERRORINFO[1] = "顏色值應為#FFFFFF的形式";
ERRORINFO[2] = "透明度的值應在0-100之間";
ERRORINFO[3] = "看缺少繪圖區";
}
#endregion
 保存圖片#region 保存圖片
 /**//// <param name="image">要保存的圖片緩沖塊</param>
/// <param name="imgFormat">圖片格式</param>
public void SaveImage(Image image, ImageFormat imgFormat)
 {
try
 {
System.Web.HttpContext content = System.Web.HttpContext.Current;
image.Save(content.Response.OutputStream,imgFormat);
}
catch
 {
throw new Exception("請引入System.Web命名空間");
}
}
 /**//// <param name="image">要保存的圖片緩沖塊</param>
/// <param name="imgFormat">圖片格式</param>
/// <param name="fileName">要保存的圖片文件名</param>
public void SaveImage(Image image, ImageFormat imgFormat, string fileName)
 {
try
 {
image.Save(fileName,imgFormat);
}
catch
 {
throw new Exception("目標文件已經存在!");
}
}
#endregion
 設定圖片的透明度#region 設定圖片的透明度
 /**//// <summary>
/// 設定圖片的透明度
/// </summary>
/// <param name="image">要設定透明度的圖片</param>
/// <param name="bgColor">底色</param>
/// <param name="alpha">透明度(0-255)</param>
public System.Drawing.Bitmap ImageAlpha(System.Drawing.Bitmap image, Color bgColor, int alpha)
 {
int iH = image.Height;
int iW = image.Width;
System.Drawing.Bitmap img = new Bitmap(iW,iH);
System.Drawing.Graphics grap = Graphics.FromImage(img);
grap.Clear(bgColor);
for(int y=0; y<iH; y++)
 {
for(int x=0; x<iW; x++)
 {
int r = image.GetPixel(x,y).R;
int g = image.GetPixel(x,y).G;
int b = image.GetPixel(x,y).B;
grap.DrawEllipse(new Pen(new SolidBrush(Color.FromArgb(alpha,r,g,b))),x,y,1,1);
}
}
return img;
}
#endregion
 跟踞數字獲得顏色#region 跟踞數字獲得顏色
public Color GetColor(int itemIndex)
 {
Color objColor;
if (itemIndex == 0)
 {
objColor = Color.Blue;
}
else if (itemIndex == 1)
 {
objColor = Color.Red;
}
else if (itemIndex == 2)
 {
objColor = Color.Yellow;
}
else if (itemIndex == 3)
 {
objColor = Color.Purple;
}
else if (itemIndex == 4)
 {
objColor = Color.Orange;
}
else if (itemIndex == 5)
 {
objColor = Color.Brown;
}
else if (itemIndex == 6)
 {
objColor = Color.Gray;
}
else if (itemIndex == 7)
 {
objColor = Color.Maroon;
}
else if (itemIndex == 8)
 {
objColor = Color.Maroon;
}
else if (itemIndex==9)
 {
objColor = Color.SlateBlue;
}
else if (itemIndex==10)
 {
objColor = Color.Gainsboro;
}
else if (itemIndex==11)
 {
objColor = Color.Khaki;
}
else if (itemIndex==12)
 {
objColor = Color.LightGoldenrodYellow;
}
else if (itemIndex==13)
 {
objColor = Color.MidnightBlue;
}
else if (itemIndex==14)
 {
objColor = Color.AntiqueWhite;
}
else if (itemIndex==15)
 {
objColor = Color.Wheat;
}
else
 {
objColor = Color.Blue;
}
return objColor;
}
#endregion
 設置錯誤信息 void SetErrorInfo(string,string,string,string)#region 設置錯誤信息 void SetErrorInfo(string,string,string,string)
 /**//// <summary>
/// 設置錯誤信息
/// </summary>
/// <param name="s">來源[1:設置屬性]</param>
/// <param name="n">來源名稱</param>
/// <param name="c">錯誤內容</param>
/// <param name="t">錯誤信息</param>
protected void SetErrorInfo(string s, string n, string c, string t)
 {
this.ErrorRecord.source = s;
this.ErrorRecord.sourcename = n;
this.ErrorRecord.content = c;
this.ErrorRecord.text = t;
}
#endregion
 刪除指定的文件 void DelFile(string)#region 刪除指定的文件 void DelFile(string)
protected void DelFile(string filename)
 {
System.IO.File.Delete(filename);
}
#endregion
 判斷文件是否存在 bool ExistsFile(string)#region 判斷文件是否存在 bool ExistsFile(string)
protected bool ExistsFile(string filename)
 {
if(System.IO.File.Exists(filename))
 {
return true;
}
else
 {
return false;
}
}
#endregion
 獲得文件的擴展名的小寫形式 string GetFile_Ext(string)#region 獲得文件的擴展名的小寫形式 string GetFile_Ext(string)
protected string GetFile_Ext(string filename)
 {
return filename.Substring(filename.LastIndexOf(".")+1).ToLower();
}
#endregion
 獲得文件格式 ImageFormat GetFileFromat(string)#region 獲得文件格式 ImageFormat GetFileFromat(string)
 /**//// <param name="fileExt">文件擴展名</param>
protected ImageFormat GetFileFromat(string fileExt)
 {
if(fileExt.ToLower()=="jpg")
return ImageFormat.Jpeg;
else if(fileExt.ToLower()=="gif")
return ImageFormat.Gif;
else
return ImageFormat.Jpeg;
}
#endregion
}
}
|