On my website, I’m receiving image files for upload. I accept files of type jpg, png, bmp or gif. If the user uploads a jpg, png or gif I want to save the file keeping the file type, but if the user uploads a bmp I want to convert it to a png.
I have a method stub like this:
private void profileImgUpload(HttpPostedFile profileImg)
HttpPostedFile has a method saveAs, to save the file, or else a property InputStream to get a stream to work with.
In the previous implementation, someone had written this:
String[] allowedExtensions = { ".png", ".jpeg", ".jpg", ".gif" };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (FileExtension == allowedExtensions[i])
FileOK = true;
}
}
if (FileOK)
profileImg.SaveAs(physicalPath + "newAvatarTemp.png");
I have assumed that this just results in the file being saved with the name newAvatarTemp.png, but the actual encoding type is not changed.
Looks like you dont convert the image, you just change the extension.
If you really want to convert check this out:
http://msdn.microsoft.com/en-us/library/9t4syfhh.aspx