how to produce thumbnails

  • Thread starter Thread starter Daniel
  • Start date Start date
D

Daniel

hi,
I have a whole lot of pictures needed to transform to the thumbnails to
display on the web, it there any easy to do?

Thanks.
 
Here's some code I've been playing around with. It creates square thumbs,
but could easily be modified to create proportional...

private void CreateThumbnail(string pathToFullSizeImage, string
pathToThumbnailImage, int sizeToCreate) {
try {
int iWidth = 0;
int iHeight = 0;
string strFilename = pathToFullSizeImage;
int dotPlace = strFilename.LastIndexOf(".");
string fileType = strFilename.Substring((dotPlace + 1),
(strFilename.Length - (dotPlace + 1))).ToUpper();

if (sizeToCreate == 1) {
iWidth = 80;
iHeight = 80;
}
else {
iWidth = 130;
iHeight = 130;
}

System.IO.FileStream fs = new System.IO.FileStream(pathToFullSizeImage,
System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.Drawing.Image objThumbnail =
System.Drawing.Image.FromStream(fs).GetThumbnailImage(iWidth, iHeight, null,
IntPtr.Zero);
// DO NOT USE THE FOLLOWING LINE (in place of the previous line) because
Image.FromFile() keeps the file open. This is why we open the file via a
stream - a stream can be closed.
// System.Drawing.Image objThumbnail =
System.Drawing.Image.FromFile(strFilename).GetThumbnailImage(iWidth,
iHeight, null, IntPtr.Zero);

// Set the ContentType correctly
if (fileType == "JPG" || fileType == "JPEG") {
objThumbnail.Save(pathToThumbnailImage, ImageFormat.Jpeg);
}
else {
objThumbnail.Save(pathToThumbnailImage, ImageFormat.Gif);
}

objThumbnail.Dispose();
fs.Close();

}

catch (Exception ex) {
if (!(ex is OOPS.DalException)) {
OOPS.WriteErrorInfo(ex, "");
}
}
}
 
Felipe, i really appreciate your help.


Here's some code I've been playing around with. It creates square thumbs,
but could easily be modified to create proportional...

private void CreateThumbnail(string pathToFullSizeImage, string
pathToThumbnailImage, int sizeToCreate) {
try {
int iWidth = 0;
int iHeight = 0;
string strFilename = pathToFullSizeImage;
int dotPlace = strFilename.LastIndexOf(".");
string fileType = strFilename.Substring((dotPlace + 1),
(strFilename.Length - (dotPlace + 1))).ToUpper();

if (sizeToCreate == 1) {
iWidth = 80;
iHeight = 80;
}
else {
iWidth = 130;
iHeight = 130;
}

System.IO.FileStream fs = new System.IO.FileStream(pathToFullSizeImage,
System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.Drawing.Image objThumbnail =
System.Drawing.Image.FromStream(fs).GetThumbnailImage(iWidth, iHeight,
null,
IntPtr.Zero);
// DO NOT USE THE FOLLOWING LINE (in place of the previous line)
because
Image.FromFile() keeps the file open. This is why we open the file via a
stream - a stream can be closed.
// System.Drawing.Image objThumbnail =
System.Drawing.Image.FromFile(strFilename).GetThumbnailImage(iWidth,
iHeight, null, IntPtr.Zero);

// Set the ContentType correctly
if (fileType == "JPG" || fileType == "JPEG") {
objThumbnail.Save(pathToThumbnailImage, ImageFormat.Jpeg);
}
else {
objThumbnail.Save(pathToThumbnailImage, ImageFormat.Gif);
}

objThumbnail.Dispose();
fs.Close();

}

catch (Exception ex) {
if (!(ex is OOPS.DalException)) {
OOPS.WriteErrorInfo(ex, "");
}
}
}
 
A good pair of thumbnail clippers.

;-)

Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
Now I know why you're an MVP : )




Kevin Spencer said:
A good pair of thumbnail clippers.

;-)

Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
Back
Top