I use following code to resize the image after i upload it. function re-sizes the images
and save them with new name without changing the file extension and image are displayed perfectly on all major browsers.
Problem is only when some one download the image and try’s opens it with any image editor like Fireworks or Photoshop it will then give following error
ERROR: *Could not open the file. Unknown file type.*
I am not sure why it give that error.
Function to resize image.
public static void ResizeImageInput(string OriginalFile, string NewFile, int NewWidth, int MaxHeight, bool OnlyResizeIfWider)
{
System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(OriginalFile);
// Prevent using images internal thumbnail
FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
if (OnlyResizeIfWider)
{
if (FullsizeImage.Width <= NewWidth)
{
NewWidth = FullsizeImage.Width;
}
}
System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(NewWidth, MaxHeight, null, IntPtr.Zero);
// Clear handle to original file so that we can overwrite it if necessary
FullsizeImage.Dispose();
// Save resized picture
NewImage.Save(NewFile);
}
Function to upload image
protected void btnArticlePageImage_Click(object sender, EventArgs e)
{
try
{
String filePath = string.Empty;
String CurrentGUID = Guid.NewGuid().ToString();
if (FileUpload2.HasFile)
{
string filename = System.IO.Path.GetFileName(FileUpload2.FileName);
System.IO.FileInfo f = new System.IO.FileInfo(FileUpload2.PostedFile.FileName);
double fileSize = (double)FileUpload2.FileBytes.Length;
if (fileSize < 1024000) // 1 MB current size size in bytes 102400=100kb 512000 = 500kb
{
if ((f.Extension.ToLower() == ".jpg") || (f.Extension.ToLower() == ".png") || (f.Extension.ToLower() == ".gif") || (f.Extension.ToLower() == ".jpeg"))
{
filename = CurrentGUID + f.Extension;
//string productGUID
filePath = Server.MapPath("../ImagesArticles/") + filename;
if (System.IO.File.Exists(filePath))
{
return;
}
else
{
//Upload files
FileUpload2.PostedFile.SaveAs(Server.MapPath("../ImagesArticles/") + filename);
//objPages.PageBannerImageEn = filename;
Session["ArticleLargeImage"] = filename.ToString();
string errMsg = "File Uploaded Successfully";
lblImageUploadMessage1.Text = errMsg;
// ResizeImage(string OriginalFile, string NewFile, int NewWidth, int MaxHeight, bool OnlyResizeIfWider)
Helper.ResizeImage(filePath, filePath, 150, 80, true);
}
return;
}
else
{
string errMsg = "File must be an Image type of .jpg, .png, .gif, .jpeg";
//client-side error
lblImageUploadMessage1.Text = errMsg;
return;
}
}
else
{
string errMsg = "File size is greater the 1MB";
//client-side error
lblImageUploadMessage1.Text = errMsg;
return;
}
}
else
{
//lblMesg.Text = "Only type .jpg, .png, .gif are allow";
string errMsg = "Cant Upload File due to some error";
//client-side error
lblImageUploadMessage1.Text = errMsg;
return;
}
}
catch (Exception ex)
{
Response.Write("ERROR MESSAGE : " + ex.Message.ToString());
}
}
Sequence: User can upload image type JPG, GIF, PNG etc. i only rename the image with GUID and keep the image extension same and then save it on the web server. After that i re size the image for later use.
These re sized images work fine in all browsers but only issue is if someone downloads these image for editing it wont open in any graphics editor tools.
It gives a common error message as mentioned in the first part of question.
Try adding the imageformat to save, this is for a gif image