locked
Converting JPEG to Tiff using bitmap.Save

    Question

  •  

    I'm trying to convert a jpeg to a tiff using the following:

     

    Dim bitmap As New Bitmap(Image.FromFile("c:\test.jpg"))

    bitmap.Save("c:\test.tif", System.Drawing.Imaging.ImageFormat.Tiff)

     

    This works however it appears to save it with a bitdepth of 32.  I think this can be changed to a bitdepth of 8 using Imaging.PixelFormat.Format8bppIndexed however I can't for the life of me figure out how to do it.

    Any suggestions?

     

    Saturday, November 04, 2006 9:14 PM

Answers

  • I've discovered a way to do this by using the GIF image encoder.  Check this thread...
    Friday, November 24, 2006 10:10 PM
    Moderator

All replies

  • Create a copy of the bitmap with Bitmap.Clone(Rectangle, PixelFormat) and save that.
    Saturday, November 04, 2006 9:34 PM
    Moderator
  • Do you mean something like this

    Dim rectangle As Rectangle
    Dim bitmap As New Bitmap(Image.FromFile("c:\test.jpg"))

    rectangle.X = 100
    rectangle.Y = 100
    rectangle.Width = bitmap.Width
    rectangle.Height = bitmap.Height

     bitmap = bitmap.Clone(Rectangle, Imaging.PixelFormat.Format8bppIndexed)
     bitmap.Save("c:\test.tif", System.Drawing.Imaging.ImageFormat.Tiff)

    With that code I get a 'System.OutOfMemoryException' error, which I don't exactly understand because the jpeg is only around 200Kb. 

    *edit*Ok I now realize that error means its outside the range of the original image and has nothing to do with actual memory usage.  I thought maybe it had something to with setting X and Y to 100 but even setting it 0 or 1 didn't help.

    Saturday, November 04, 2006 10:14 PM
  • Dang it, GDI+ is a pain in the rear end when dealing with paletted (indexed) color tables.  You can't use Graphics.FromImage() either.  Sorry, you can't do this easily with .NET I'm afraid.  The really hard way is that you create your own palette of 256 colors that represent the original image's colors as closely as possible, then translate the image pixels one by one.  Nah, use a professional graphics library to do that, something like Lead Tools...
    Saturday, November 04, 2006 11:27 PM
    Moderator
  • I've discovered a way to do this by using the GIF image encoder.  Check this thread...
    Friday, November 24, 2006 10:10 PM
    Moderator