Last active
December 18, 2015 13:08
-
-
Save sphingu/5787296 to your computer and use it in GitHub Desktop.
Generate Image Thumbnail
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//File Upload | |
// If file field isn’t empty | |
if (filUpload.PostedFile != null) | |
{ | |
// Check file size (mustn’t be 0) | |
HttpPostedFile myFile = filUpload.PostedFile; | |
int nFileLen = myFile.ContentLength; | |
if (nFileLen == 0) | |
{ | |
return; | |
} | |
// Check file extension (must be JPG) | |
if (System.IO.Path.GetExtension(myFile.FileName).ToLower() != ".jpg") | |
{ | |
return; | |
} | |
// Read file into a data stream | |
byte[] myData = new Byte[nFileLen]; | |
myFile.InputStream.Read(myData, 0, nFileLen); | |
// Make sure a duplicate file doesn’t exist. If it does, keep on appending an incremental numeric until it is unique | |
string sFilename = System.IO.Path.GetFileName(myFile.FileName); | |
int file_append = 0; | |
while (System.IO.File.Exists(Server.MapPath(sSavePath + sFilename))) | |
{ | |
file_append++; | |
sFilename = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) + file_append.ToString() + ".jpg"; | |
} | |
// Save the stream to disk | |
System.IO.FileStream newFile = new System.IO.FileStream(Server.MapPath(sSavePath + sFilename), System.IO.FileMode.Create); | |
newFile.Write(myData, 0, myData.Length); | |
newFile.Close(); | |
// Check whether the file is really a JPEG by opening it | |
System.Drawing.Image.GetThumbnailImageAbort myCallBack = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback); | |
Bitmap myBitmap; | |
myBitmap = new Bitmap(Server.MapPath(sSavePath + sFilename)); | |
// If jpg file is a jpeg, create a thumbnail filename that is unique. | |
file_append = 0; | |
string sThumbFile = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) + sThumbExtension + ".jpg"; | |
while (System.IO.File.Exists(Server.MapPath(sSavePath + sThumbFile))) | |
{ | |
file_append++; | |
sThumbFile = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) + file_append.ToString() + sThumbExtension + ".jpg"; | |
} | |
// Save thumbnail and output it onto the webpage | |
System.Drawing.Image myThumbnail = myBitmap.GetThumbnailImage(intThumbWidth, intThumbHeight, myCallBack, IntPtr.Zero); | |
myThumbnail.Save(Server.MapPath(sSavePath + sThumbFile)); | |
imgPicture.ImageUrl = sSavePath + sThumbFile; | |
// Displaying success information | |
// Destroy objects | |
myThumbnail.Dispose(); | |
myBitmap.Dispose(); | |
} | |
public bool ThumbnailCallback() | |
{ | |
return false; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected void btnsave_Click(object sender, EventArgs e) | |
{ | |
string filename = Path.GetFileName(fileupload1.PostedFile.FileName); | |
string targetPath = Server.MapPath("Images/" + filename); | |
Stream strm = fileupload1.PostedFile.InputStream; | |
var targetFile = targetPath; | |
//Based on scalefactor image size will vary | |
GenerateThumbnails(0.5, strm, targetFile); | |
BindDataList(); | |
} | |
private void GenerateThumbnails(double scaleFactor, Stream sourcePath, string targetPath) | |
{ | |
using (var image = Image.FromStream(sourcePath)) | |
{ | |
var newWidth = (int)(image.Width * scaleFactor); | |
var newHeight = (int)(image.Height * scaleFactor); | |
var thumbnailImg = new Bitmap(newWidth, newHeight); | |
var thumbGraph = Graphics.FromImage(thumbnailImg); | |
thumbGraph.CompositingQuality = CompositingQuality.HighQuality; | |
thumbGraph.SmoothingMode = SmoothingMode.HighQuality; | |
thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic; | |
var imageRectangle = new Rectangle(0, 0, newWidth, newHeight); | |
thumbGraph.DrawImage(image, imageRectangle); | |
thumbnailImg.Save(targetPath, image.RawFormat); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment