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.Format 1bppIndexe d 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(DirectC ast(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.Format 1bppIndexe d)
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(Direct ory.GetCur rentDirect ory()).Par ent.FullNa me + "\files\"
Dim inImage As String = Path.Combine(thePath, inFile)
ResizeAndSave03(inImage, Path.Combine(thePath, outFile03))
End Sub
I have read to use Imaging.PixelFormat.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(DirectC
'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.Format
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(Direct
Dim inImage As String = Path.Combine(thePath, inFile)
ResizeAndSave03(inImage, Path.Combine(thePath, outFile03))
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You're welcome. Good luck with your project! Regards, Joe
ASKER