国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

asp.net中生成縮略圖并添加版權實例代碼

2024-07-10 13:15:07
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:


//定義image類的對象
Drawing.Image image,newimage;
//圖片路徑
protected string imagePath;
//圖片類型
protected string imageType;
//圖片名稱
protected string imageName;
//提供一個回調方法,用于確定Image對象在執行生成縮略圖操作時何時提前取消執行
//如果此方法確定 GetThumbnailImage 方法應提前停止執行,則返回 true;否則返回 false
System.Drawing.Image.GetThumbnailImageAbort callb = null;

private void sm_Click(object sender, System.EventArgs e)
{
string mPath;

if("" != File1.PostedFile.FileName) //File1為上傳文件控件
{
imagePath = File1.PostedFile.FileName;
//取得圖片類型
imageType= imagePath.Substring(imagePath.LastIndexOf(".")+1);
//取得圖片名稱
imageName = imagePath.Substring(imagePath.LastIndexOf("http://")+1);
//判斷是否是JPG或者GIF圖片,這里只是舉個例子,并不一定必須是這兩種圖片
if("jpg" != imageType && "gif" != imageType)
{
Response.Write("<script language='javascript'> alert('對不起!請您選擇jpg或者gif格式的圖片!');</script>");
return;
}
else
{
try
{
//建立虛擬路徑
mPath=Server.MapPath("UploadFiles");
//保存到虛擬路徑
File1.PostedFile.SaveAs(mPath+"http://"+imageName);

//顯示原圖, imageSource為圖片控件
//imageSource.ImageUrl = "UploadFiles/"+imageName;

//為上傳的圖片建立引用
image=System.Drawing.Image.FromFile(mPath+"http://"+imageName);
//生成縮略圖
newimage=image.GetThumbnailImage(200,200,callb,new System.IntPtr());
//把縮略圖保存到指定的虛擬路徑
newimage.Save(Server.MapPath("UploadFiles")+"http://small"+imageName);
//釋放image對象占用的資源
image.Dispose();
//釋放newimage對象的資源
newimage.Dispose();
//顯示縮略圖

AddTextToImg ("UploadFiles/"+"small"+imageName,"Pic Info"); // 在圖片上加入信息說明
Image1.ImageUrl = "UploadFiles/"+"small"+imageName;

Script.Alert("上傳成功!");
}
catch
{
Script.Alert("上傳失敗!");
}

} // end else
}

// 在圖片上加入自己的信息,
// AddTextToImg (physicPath,"Pic Info");
private void AddTextToImg(string fileName,string text)
{
//string sss = MapPath(fileName);

if ( !File.Exists ( fileName)) {
throw new FileNotFoundException("The file don't exist!");
}

//還需要判斷文件類型是否為圖像類型,這里就不贅述了

System.Drawing.Image image = System.Drawing.Image.FromFile(fileName);//MapPath(fileName));
Bitmap bitmap = new Bitmap(image,image.Width,image.Height);
Graphics g = Graphics.FromImage(bitmap);

float fontSize = 22.0f; //字體大小
float textWidth = text.Length*fontSize; //文本的長度
//下面定義一個矩形區域,以后在這個矩形里畫上白底黑字
float rectX = 0;
float rectY = 0;
float rectWidth = text.Length*(fontSize+18);
float rectHeight = fontSize+18;
//聲明矩形域
RectangleF textArea = new RectangleF(rectX,rectY,rectWidth,rectHeight);
Font font = new Font("宋體",fontSize);//定義字體
Brush whiteBrush = new SolidBrush(Color.White);
Brush blackBrush = new SolidBrush(Color.Black);
g.FillRectangle(blackBrush,rectX,rectY,rectWidth,rectHeight);
g.DrawString(text,font,whiteBrush,textArea);
MemoryStream ms = new MemoryStream();
//保存為Jpg類型
bitmap.Save(ms,ImageFormat.Jpeg);

//輸出處理后的圖像,這里為了演示方便,我將圖片顯示在頁面中了
/**//* Response.Clear();
Response.ContentType = "image/jpeg";
Response.BinaryWrite( ms.ToArray() );
*/
FileStream fs=new FileStream(fileName, FileMode.OpenOrCreate);//.CreateNew);
fs.Write(ms.ToArray(),0,ms.ToArray().Length);
fs.Close();

Image1.ImageUrl = fileName; // 將圖片顯示在Image控件中
g.Dispose();
bitmap.Dispose();
image.Dispose();
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 皋兰县| 衡阳县| 成武县| 陵川县| 综艺| 邯郸市| 瑞昌市| 黔南| 中超| 屯门区| 梧州市| 诏安县| 岗巴县| 巨鹿县| 阳城县| 西贡区| 大宁县| 青冈县| 陇川县| 塔河县| 邳州市| 博白县| 盐边县| 融水| 时尚| 新密市| 拉萨市| 四川省| 青川县| 罗甸县| 肥城市| 色达县| 偃师市| 定远县| 霍城县| 田阳县| 包头市| 马鞍山市| 渭南市| 渑池县| 乐都县|