Multiframe Tiff is too big RRS feed

  • Question

  • User185754410 posted



    I'm trying to open a multiframe tif and write something inside. The code works fine, the problem is that the file is too big.  For example a 1 Mb file becomes a 21 Mb file. I have tried changing ColorDepth or Compression Parameters but either it doesn't work or the following error comes out:

    A generic error occurred in GDI+.

    Below all the code in my page.

    Do you have any idea?




    <%@ Page Language="VB" %>
    <%@ import Namespace="System.IO" %>
    <%@ import Namespace="System.Drawing" %>
    <%@ import Namespace="System.Drawing.Imaging" %>
    <%@ import Namespace="System.Drawing.Drawing2D" %>


    ' initialise objects
    Dim strFilename as string
    'Dim g as System.Drawing.Image

    ' set the filename
    strFilename = "c:\test.tif"

    dim b as System.Drawing.Image
    b = System.Drawing.Image.FromFile(strFilename)

            Dim info As ImageCodecInfo = Nothing
            Dim ice As ImageCodecInfo
            For Each ice In ImageCodecInfo.GetImageEncoders()
                    If ice.MimeType = "image/tiff" Then
                        info = ice
                End If

            Next ice 'use the save encoder
           ' response.end
           Dim enc As System.Drawing.Imaging.Encoder = System.Drawing.Imaging.Encoder.SaveFlag
           Dim ep As New EncoderParameters(2)
            ep.Param(0) = New EncoderParameter(enc, CLng(EncoderValue.MultiFrame))
            ep.Param(1) = New EncoderParameter(System.Drawing.Imaging.Encoder.Compression, Fix(EncoderValue.CompressionLZW))
    '     ep.Param(2) = New EncoderParameter(System.Drawing.Imaging.Encoder.ColorDepth, 1L)       
    '  ep.Param(3) = New EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 1L)       
            Dim pages As Bitmap = Nothing

    Dim frameDim As System.Drawing.Imaging.FrameDimension
    frameDim = New System.Drawing.Imaging.FrameDimension(b.FrameDimensionsList(0))

    'display number of frames with code below
    response.write("Frame Count: " & b.GetFrameCount(frameDim))


    Dim f as integer
    for f = 0 to b.GetFrameCount(frameDim)-1

    Dim g As Graphics = Nothing
    dim bmp as new System.Drawing.Bitmap(b)

     if f=0 then
     ' display an individual frame 
     Dim rectangleFont As New Font("Arial Narrow", 14, FontStyle.Bold)
      g = Graphics.FromImage(bmp)

       ' Use the Graphics object to write a string
       ' on the rectangles.
      g.DrawString("HELLOOOO", rectangleFont, SystemBrushes.WindowText, New PointF(2, 1))

        pages = bmp

                    'save the first frame
                    pages.Save("c:\temp\test2.tif", info, ep)

        ep.Param(0) = New EncoderParameter(enc, CLng(EncoderValue.FrameDimensionPage)) 
        ep.Param(1) = New EncoderParameter(System.Drawing.Imaging.Encoder.Compression, Fix(EncoderValue.CompressionLZW)) 
    '    ep.Param(2) = New EncoderParameter(System.Drawing.Imaging.Encoder.ColorDepth, 1L)            
    '  ep.Param(3) = New EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 1L)
       pages.SaveAdd(bmp, ep)
    '  bmp.Dispose()
     end if
      If f = b.GetFrameCount(frameDim)-1 Then
                'flush and close.
             ep.Param(0) = New EncoderParameter(enc, CLng(EncoderValue.Flush))
             ep.Param(1) = New EncoderParameter(System.Drawing.Imaging.Encoder.Compression, Fix(EncoderValue.CompressionLZW)) 
        '     ep.Param(2) = New EncoderParameter(System.Drawing.Imaging.Encoder.ColorDepth, 1L)       
      '  ep.Param(3) = New EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 1L)
         End If




    Thursday, December 29, 2005 1:11 PM

All replies

  • User1998001011 posted

    I am having the exact same problem. Did you ever get around this?

    Ideally, you'd get image format info from the original tiff and use it to store the new one. No clue as to how this is done, though.

    Anyone out there have an idea?


    Friday, March 3, 2006 4:26 PM