|
|
|
จากตัวอย่างที่มีให้ศึกษาเรื่องการ upload รูป และใส่ WaterMark ผมติดปัญหาอ่าครับ |
|
|
|
|
|
|
|
จากตัวอย่างที่มีให้ศึกษาเรื่องการ upload รูป และใส่ WaterMark ผมติดปัญหาอ่าครับคือผมรวมให้มัน Upload ได้หลายๆรูปแล้ว resize แล้ว ใส่ Watermark ด้วยแต่มัน error และ ฟ้อง A generic error occurred in GDI+. ครับผมต้องแก้ไขยังไงอ่าครับ ขอบคุณล่วงหน้าครับ
code ตามนี้ครับ
Code (C#)
void ddlNum_Changed(object sender, System.EventArgs e)
{
int i = 0;
FileUpload fileUpload;
Label lblBr;
for (i = 1; i <= Convert.ToInt32(this.ddlNum.SelectedItem.Value); i++) {
fileUpload = new FileUpload();
{
fileUpload.ID = "fiUpload" + i;
}
lblBr = new Label();
lblBr.Text = "<br>";
this.pnlCtrl.Controls.Add(fileUpload);
this.pnlCtrl.Controls.Add(lblBr);
}
if (Convert.ToInt32(this.ddlNum.SelectedItem.Value) > 0) {
this.pnlUpload.Visible = true;
}
else {
this.pnlUpload.Visible = false;
this.form1.Enctype = "multipart/form-data";
}
}
void btnUpload_OnClick(object sender, EventArgs e)
{
int i = 0;
HttpFileCollection myUpoad = Request.Files;
HttpPostedFile myFiles;
string FileName = null;
string NewFileName = null;
for (i = 0; i <= Convert.ToInt32(this.ddlNum.SelectedItem.Value) - 1; i++) {
myFiles = myUpoad[i];
//*** Check Control ***//
if (myUpoad.Keys[i].ToString() == ("fiUpload" + (i + 1)).ToString()) {
if (!string.IsNullOrEmpty((myFiles.FileName))) {
FileName = "MyUpload/" + System.IO.Path.GetFileName(myFiles.FileName);
NewFileName = "MyUpload/Thumbnail_" + System.IO.Path.GetFileName(myFiles.FileName);
//*** Upload Original Images ***//
myFiles.SaveAs(Server.MapPath(FileName));
//*** Call to function resize ***//
WriteWaterMark(FileName);
ResizeImages(FileName, NewFileName);
this.lblText.Text = this.lblText.Text + System.IO.Path.GetFileName(myFiles.FileName) + " uploaded & resize.<br>";
}
}
}
this.ddlNum.Visible = false;
this.pnlUpload.Visible = false;
}
void ResizeImages(object FileName, object NewFileName)
{
int intWidth = 0;
int intHeight = 0;
intWidth = 130;
//*** Fix Width ***//
intHeight = 100;
//*** If = 0 Auto Re-Cal Size ***//
System.Drawing.Image objGraphic = System.Drawing.Image.FromFile(Server.MapPath(FileName.ToString()));
Bitmap objBitmap;
//*** Calculate Height ***//
if (intHeight > 0) {
objBitmap = new Bitmap(objGraphic, intWidth, intHeight);
}
else {
if (objGraphic.Width > intWidth) {
double ratio = objGraphic.Height / objGraphic.Width;
intHeight = (int)ratio * (int)intWidth;
objBitmap = new Bitmap(objGraphic, intWidth, intHeight);
}
else {
objBitmap = new Bitmap(objGraphic);
}
}
//*** Save As ***//
objBitmap.Save(Server.MapPath(NewFileName.ToString()), objGraphic.RawFormat);
//*** Close ***//
objGraphic.Dispose();
//*** Nothing ***//
objBitmap = null;
objGraphic = null;
}
void WriteWaterMark(object FileName)
{
Bitmap objBitmap = new System.Drawing.Bitmap(Server.MapPath(FileName.ToString()));
Graphics objGraphic = Graphics.FromImage(objBitmap);
//*** Write Text ***//
string strText = "CopyRight 2009 (www.ThaiCreate.Com)";
//*** String ***//
Font objFont = new Font("Tahoma", 10);
//*** Font Style ***//
SolidBrush objBrushWrite = new SolidBrush(Color.Red);
//*** Font Color ***//
//*** Text Align ***//
StringFormat strFormat = new StringFormat();
strFormat.Alignment = StringAlignment.Far;
//*** .Near (Left) , .Far (Right) ***//
strFormat.LineAlignment = StringAlignment.Far;
//*** .Near (Top) , .Far (Down) ***//
//*** DrawString ***//
objGraphic.DrawString(strText, objFont, objBrushWrite, new Rectangle(0, 0, objBitmap.Width, objBitmap.Height), strFormat);
//*** Save As ***//
objBitmap.Save(Server.MapPath(FileName.ToString()), ImageFormat.Jpeg); ******* ฟ้อง error ตรงนี้ครับ *******
//*** Close ***//
objGraphic.Dispose();
//*** Nothing ***//
objBitmap = null;
objGraphic = null;
}
Tag : .NET, Ms SQL Server 2005, Web (ASP.NET), C#
|
|
|
|
|
|
Date :
2012-03-07 20:43:56 |
By :
yatard |
View :
1229 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ครับ เดี๋ยวผมลองดูครับขอบคุณครับ
|
|
|
|
|
Date :
2012-03-08 12:24:46 |
By :
yatard |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|