PDA

View Full Version : مشکل در ذخیره عکس Thumbnail در سایت



sanaz.dadkhah
پنج شنبه 20 بهمن 1390, 12:18 عصر
سلام من میخواهم که یه گالری دینامیک بسازم و نیاز دارم که عکس هام را هم با اندازه کوچک دخیره کنم.ی کد از کد پروجکت گرفتم که اندازه را عوض میکنه و لی Save نمی کنه


// get the file name -- fall800.jpg
string file = Request.QueryString["file"];
// create an image object, using the filename we just retrieved
System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(file) );
// create the actual thumbnail image
System.Drawing.Image thumbnailImage = image.GetThumbnailImage(64, 64, new System.Drawing.Image.GetThumbnailImageAbort(Thumbn ailCallback), IntPtr.Zero);
// make a memory stream to work with the image bytes
MemoryStream imageStream = new MemoryStream();
// put the image into the memory stream
thumbnailImage.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);
// make byte array the same size as the image
byte[] imageContent = new Byte[imageStream.Length];
// rewind the memory stream
imageStream.Position = 0;
// load the byte array with the image
imageStream.Read(imageContent, 0, (int)imageStream.Length);
// return byte array to caller with image type
Response.ContentType = "image/jpeg";
Response.BinaryWrite(imageContent);

این قسمت کد کار هم میده اما نفهمیدم عکس حاصل که در آرایه است را با چه متدی در ی فایل جدید save کنم میشه بگین با چه متدی میتونم این کارو انجام بدهم

ramin149
پنج شنبه 20 بهمن 1390, 12:25 عصر
public bool GroupImages(int width, int height, HiddenField addrees, FileUpload fileup, Label Error)
{
string mypath = HttpContext.Current.Server.MapPath("~") + "/Administrator/Images/Group/";
string oldphoto = HttpContext.Current.Server.MapPath("~") + "/Administrator/Images/Group/Old/";

if (!Directory.Exists(HttpContext.Current.Server.MapP ath("~/Administrator/Images/Group/")))
{
Directory.CreateDirectory(HttpContext.Current.Serv er.MapPath("~/Administrator/Images/Group/"));
}
if (!Directory.Exists(HttpContext.Current.Server.MapP ath("~/Administrator/Images/Group/Old")))
{
Directory.CreateDirectory(HttpContext.Current.Serv er.MapPath("~/Administrator/Images/Group/Old"));
}

string[] validextention = { ".jpg", ".png", ".bnp", ".jpeg", ".gif" };
string x = System.IO.Path.GetExtension(fileup.PostedFile.File Name);
if (Array.IndexOf(validextention, x.ToLower()) < 0)
{
Error.Text = "you seleted invalid type of image for uploading";
return false;
}


long sizefile = fileup.PostedFile.ContentLength;
sizefile = sizefile / 1024;
if (sizefile > 30000)
{
Error.Text = "invalid size image uploading";
return false;
}

string filename = System.IO.Path.GetFileName(fileup.PostedFile.FileN ame);
while (System.IO.File.Exists(mypath + filename))
{
int c = filename.Length - x.Length;
filename = filename.Insert(c, "3");

}
fileup.PostedFile.SaveAs(oldphoto + filename);


System.Drawing.Image sphotos;
sphotos = System.Drawing.Image.FromFile((oldphoto + filename));
Bitmap convert = new Bitmap(sphotos, width, height);
object imageformat = sphotos.RawFormat;
convert.Save(mypath + filename);
addrees.Value = "~/Administrator/Images/Group/" + filename;

Error.Text = "عکس ذخیره شد";

sphotos.Dispose();
sphotos = null;
convert.Dispose();
convert = null;
fileup.Dispose();
fileup = null;
System.IO.File.Delete(oldphoto + filename);
return true;
}

sanaz.dadkhah
پنج شنبه 20 بهمن 1390, 12:31 عصر
میشه ی توضیح بدین
اصلا من ی عکس دارم با اندازه مثال 800*600 که کاربر میده میخام که ی عکس با اندازه 200*300 بسازم و ذخیره کنم
چیکار کنم لطفا راهنمایی کنید

ramin149
پنج شنبه 20 بهمن 1390, 12:36 عصر
تابعی رو که براتون گذاشتم دو مقدار عدد می گیره به نام width height که به جای آن اندازه طول و عرض عکستون رو دهید .
در تابع بالا هم چند خط کد زیر این عملیات را انجام می دهند .

System.Drawing.Image sphotos; sphotos = System.Drawing.Image.FromFile((oldphoto + filename));
Bitmap convert = new Bitmap(sphotos, width, height);
object imageformat = sphotos.RawFormat;
convert.Save(mypath + filename);

fakhravari
پنج شنبه 20 بهمن 1390, 13:11 عصر
با سلام
برای چک کردن فرمت
pathImage = Server.MapPath(@"img/");
String[] validext = { ".jpg", ".jpeg", ".bmp", ".jpe", ".png", ".gif", ".tiff", ".tif", ".bmp" };
string ext = System.IO.Path.GetExtension(FileUpload1.PostedFile .FileName);
if (Array.IndexOf(validext, ext.ToLower()) < 0)
{
Label1.Text = "Eroor > FileUpload1.PostedFile.ContentType";
return;
}
else
{
string filetype = FileUpload1.PostedFile.ContentType.ToString();
if (filetype == "image/x-png" || filetype == "image/gif" || filetype == "image/pjpeg")
{
ImageFileName = System.IO.Path.GetFileName(FileUpload1.FileName);

while (System.IO.File.Exists(pathImage + ImageFileName))
{
ImageFileName = "1" + ImageFileName;
}

FileUpload1.PostedFile.SaveAs(pathImage + ImageFileName);
}
else
{
Label1.Text = "Eroor > FileUpload1.PostedFile.ContentType";
return;
}
}

fakhravari
پنج شنبه 20 بهمن 1390, 15:55 عصر
ببنید راه حل ساده تر از روش بالا به صورت زیر است
string mypath = HttpContext.Current.Server.MapPath("~") + "/Administrator/Images/Group/";

System.Drawing.Image sphotos;
sphotos = System.Drawing.Image.FromFile(FileUpload1.PostedFi le.FileName);

Bitmap convert = new Bitmap(sphotos, 500, 200);
object imageformat = sphotos.RawFormat;
convert.Save(mypath + FileUpload1.FileName);

taherisoftware
جمعه 21 بهمن 1390, 13:50 عصر
برای استفاده از bitmap چه فضای نامی رو باید اضافه کنیم؟؟؟؟؟

taherisoftware
جمعه 21 بهمن 1390, 13:52 عصر
هیچی فهمیدم!!!!!!!!!!!!!
system.drawing

taherisoftware
جمعه 21 بهمن 1390, 14:00 عصر
این کد رو باید کجا بنویسم؟؟؟؟؟؟؟؟
string mypath = HttpContext.Current.Server.MapPath("~") + "/Administrator/Images/Group/";

System.Drawing.Image sphotos;
sphotos = System.Drawing.Image.FromFile(FileUpload1.PostedFi le.FileName);

Bitmap convert = new Bitmap(sphotos, 500, 200);
object imageformat = sphotos.RawFormat;
convert.Save(mypath + FileUpload1.FileName);

fakhravari
جمعه 21 بهمن 1390, 14:33 عصر
یه باتن بزارید با یه fileupload توی codbehin باتن
این پوشه ها هم ایجاد کنید Administrator/Images/Group

taherisoftware
جمعه 21 بهمن 1390, 15:48 عصر
ولی توی این خط خطای filenotfound exeption رو میگیره
sphotos = System.Drawing.Image.FromFile(FileUpload1.PostedFi le.FileName);

fakhravari
جمعه 21 بهمن 1390, 22:32 عصر
ببنید به این شکل استفاده کنید.
public static string Upload_ReSize(string MyPath,int Size, FileUpload fu, int Width, int Height)
{
if (fu.FileName.Length <= Size)
{
System.Drawing.Image sphotos;
sphotos = System.Drawing.Image.FromFile(fu.PostedFile.FileNa me);

Bitmap convert = new Bitmap(sphotos, Width, Height);
object imageformat = sphotos.RawFormat;
convert.Save(MyPath + fu.FileName);
}
return fu.FileName;
}

protected void Button4_Click(object sender, EventArgs e)
{
Upload_ReSize(Server.MapPath("~/"), 0, FileUpload1, 400, 400);
}