PDA

View Full Version : نمایش عکس در File upload control



hasty0087
سه شنبه 08 اسفند 1391, 23:47 عصر
سلام.
با استفاده از کنترل FileUpload تونستم فایل تصویری رو اپلود کنم و می خوام همزمان در یک کنترل Image، تصویر انتخاب شده به کاربر نشون داده بشه، چه باید کرد؟ protected void Button1_Click(object sender, EventArgs e)
{
if( (FileUpload1.HasFile)&&(FileUpload1.PostedFile.ContentType == "image/jpeg")&&(FileUpload1.PostedFile.ContentLength < 102400))
{

string savepath = Server.MapPath("~/UploadedImage/");
string filename = FileUpload1.FileName;
savepath += filename;
FileUpload1.PostedFile.SaveAs(savepath);
}
else
Response.Write("لطفا فایلی را انتخاب نمایید");
}

mariakhanom
چهارشنبه 09 اسفند 1391, 00:11 صبح
protected void Button1_Click(object sender, EventArgs e)

{



if(FileUpload1.HasFile){



string FullName = FileUpload1.PostedFile.FileName;



if (this.CheckFileType(FullName, FileUpload1.PostedFile.ContentType))//معتبربودن عکس

{



byte[] img = FileUpload1.FileBytes;



MemoryStream str = new MemoryStream();



Bitmap upBmp = new Bitmap(str);



if (upBmp.Width == 236 & upBmp.Height == 337)//اندازه عکس

{

str.Write(img, 0, img.Length);



Bitmap upBmp2 = new Bitmap(str);



//تنظیمات نمایشی عکس



Bitmap newBmp = new Bitmap(236, 337, System.Drawing.Imaging.PixelFormat.Format24bppRgb) ;

newBmp.SetResolution(72, 72);



Graphics newGraphic = Graphics.FromImage(newBmp);

newGraphic.Clear(

Color.Empty);

newGraphic.DrawImage(upBmp2, 0, 0,236, 337);



String upName = FileUpload1.FileName.Substring(0, FileUpload1.FileName.IndexOf("."));



String filePath = "~/NewFolder1/ " + upName + ".jpeg";//مسیر ذخیره سازی به همراه نام و پسوند عکس

newBmp.Save(Server.MapPath(filePath), System.Drawing.Imaging.

ImageFormat.Jpeg);

Image2.ImageUrl = filePath;

Label1.Text =

"اطلاعات با موفقیت ثبت شد ";

}



else

Label1.Text =

"لطفا عکس 4*3 انتخاب نمایید ";

}



else

Label1.Text =

"انتخاب نماییدbmp , gif , jpeg , jpg لطفا فایل با پسوند ";

}

}

mariakhanom
چهارشنبه 09 اسفند 1391, 00:26 صبح
آهان.تابع CheckFileType




public bool CheckFileType(string FullName, string MimeType)

{



string ext = Path.GetExtension(FullName);



switch (ext.ToLower())

{



case ".gif":





if (MimeType.ToLower() == "image/gif")

{



return true;

}



else

{



return false;

}



case ".png":



if (MimeType.ToLower() == "image/png")

{



return true;

}



else if (MimeType.ToLower() == "image/x-png")

{



return true;

}



else

{



return false;

}





case ".jpg":



if (MimeType.ToLower() == "image/jpeg")

{



return true;

}



else if (MimeType.ToLower() == "image/pjpeg")

{



return true;

}



else

{



return false;

}





case ".jpeg":



if (MimeType.ToLower() == "image/jpeg")

{



return true;

}



else if (MimeType.ToLower() == "image/pjpeg")

{



return true;

}



else

{



return false;

}





case ".bmp":



if (MimeType.ToLower() == "image/bmp")

{



return true;

}



else

{



return false;

}



default:



return false;

}

}