Saturday, September 26, 2009

Convert TIF file to JPG(s) - ASP.NET

Sometimes showing a buch of JPG images is easier than a TIF file on your site, or sometimes you want to show the first page of a TIF file to give the better sense of the contain of TIF file, so you see this Conversion help you for more suitable designing your asp.net applicaion.
For creating a jpg file ftom a multiple page tif file you shouldn't go though While loop and just convert the first page.

Note: In this example I could'nt use "<" or ">" tage and use "/" instead :)
public class ConvertImages
{
public ConvertImages()
{
}
///
/// This function return list of images which is converted from a TIF file
/// for using this function,the best way to itarate on returned list is using for each statement
/// within the iteration you should Dispose each image.
///

///


Full path of TIF file such as :\Temp\MyFile.tif
///


the path folder which the images should be create
///
public static List /Image/ ConvertTIFtoJPGs(string TIFFileFullName,string OutPutFolder)
{
System.Drawing.Image objImage,NewImage;
Guid objGuid;
System.Drawing.Imaging.FrameDimension objDimension;
List ImageList;
string OutputFileName;
int TotalFrames = 0;
int ItemIndex = 0;
objImage = Image.FromFile(TIFFileFullName);
objGuid = objImage.FrameDimensionsList[0];
objDimension = new System.Drawing.Imaging.FrameDimension(objGuid);
TotalFrames = objImage.GetFrameCount(objDimension);
ImageList = new List / image/ (TotalFrames);
while (ItemIndex < TotalFrames) { objImage.SelectActiveFrame(objDimension, ItemIndex); OutputFileName = OutPutFolder + @"\" + Guid.NewGuid().ToString() + ".JPG"; objImage.Save(OutputFileName, System.Drawing.Imaging.ImageFormat.Jpeg); NewImage = Image.FromFile(OutputFileName); ImageList.Add(NewImage); ItemIndex++; } objImage.Dispose(); return ImageList; } } //Here I write a test procedure in another class to test above class
private void Test_TIF2JPGConversion()
{

List/Image/ imageList;
imageList =ConvertImages.ConvertTIFtoJPGs(@"C:\Temp\MyTIF.JPG", C:\Temp\Images"); foreach (Image imageItem in imageList)
{

//Do something with images, such as storing or anything else

//Remember to dispose each image
imageItem.Dispose();
}

No comments:

Post a Comment

Thank you for sharing your knowledge and experiences with this weblog.