Link to home
Start Free TrialLog in
Avatar of wilfordrocks
wilfordrocks

asked on

Reduce Tiff file size. Need to set bit dept to 1.

I want to reduce the image of a tiff by about 1/2.  Or maybe I just want to change the DPI from 300 to 96.  Either solution would be fine.  The method below takes a tiff and makes it larger because the bit depth changes from 1 to 32.  It does change the DBI from 300 to 96 and that is desirable.

I have read to use Imaging.PixelFormat.Format1bppIndexed in the constructor, but that creates the exception A Graphics object cannot be created from an image that has an indexed pixel format.

Thank you for your suggestions.


Method 1.
Public Sub ResizeAndSave03(ByVal ImagePath As String, ByVal SavePath As String)

        If File.Exists(SavePath) = True Then
            File.Delete(SavePath)
        End If

        Dim b As New Bitmap(ImagePath)
        Dim Width As Int32 = Convert.ToInt32(b.Width / 2)
        Dim Height As Int32 = Convert.ToInt32(b.Height / 2)
        Dim result As New Bitmap(Width, Height)
        Using g As Graphics = Graphics.FromImage(DirectCast(result, Image))
            'Will throw  - A Graphics object cannot be created from an image that has an indexed pixel format.
            'when this constructor used  Dim result As New Bitmap(Width, Height, Imaging.PixelFormat.Format1bppIndexed)
            g.DrawImage(b, 0, 0, Width, Height)
        End Using
        result.Save(SavePath)
    End Sub

Method 2.

 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim inFile = "test01.tif"
        Dim outFile03 = "outFile03.tif"
        Dim thePath As String = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName + "\files\"
        Dim inImage As String = Path.Combine(thePath, inFile)
              ResizeAndSave03(inImage, Path.Combine(thePath, outFile03))

    End Sub
ASKER CERTIFIED SOLUTION
Avatar of Joe Winograd
Joe Winograd
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wilfordrocks
wilfordrocks

ASKER

Thank you for a very detailed response.
You're welcome. Good luck with your project! Regards, Joe