Friday, 21 February 2014

How to create or extract images from slides of .tiff image using C# ,DotNet

Creating images from slides of Tiff image:

#region "Description"
/*  
 * Tiff image slides can be exported into images using CreateImagesFromTiffImage() method given below  
 */
#endregion

#region "Methods"


Method Name : CreateImagesFromTiffImage()
/// <summary>
/// Given Function takes input as Tiff image file stream, image saving folder path and returns created images path list
/// </summary>
/// <param name="SourceFileStream">Tiff image file stream</param>
/// <param name="SavingPath">Image saving path</param>
/// <returns>List of paths for created images in order</returns>

 public static Dictionary<int, string> CreateImagesFromTiffImage(Stream SourceFileStream, string SavingPath)
{
    Dictionary<int, string> pageImages = new Dictionary<int, string>();
    int pagecount = 0;
    int ImagePageCount = 0;
    System.Drawing.Image oimage = null;
    System.Drawing.Bitmap fImage = null;
    bool pageLoad = false;

    string docPath = SavingPath + "\\";
    string filename = Guid.NewGuid().ToString();
    string documentPath = "";
    try
    {
        fImage = new System.Drawing.Bitmap(SourceFileStream, false);
        oimage = fImage;
        pagecount = oimage.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page);
        pageLoad = true;
    }
    catch (Exception ex)
    {
        pageLoad = false;       
    }
    if (pageLoad == true)
    {
        if (pagecount != 0)
        {
            for (ImagePageCount = 0; ImagePageCount <= pagecount - 1; ImagePageCount++)
            {  
                oimage.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, ImagePageCount);

                System.Drawing.Bitmap img1 = (System.Drawing.Bitmap)oimage.Clone();
                documentPath = docPath + filename + (ImagePageCount + 1).ToString() + ".jpg";
                img1.Save(documentPath, System.Drawing.Imaging.ImageFormat.Jpeg);
            
                if (!pageImages.ContainsKey(ImagePageCount))
                    pageImages.Add(ImagePageCount, string.Empty);
                pageImages[ImagePageCount] = documentPath;
            }
        }
        oimage.Dispose();
        fImage.Dispose();
    }
    return pageImages;
}
#endregion



No comments:

Post a Comment