By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,267 Members | 1,861 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,267 IT Pros & Developers. It's quick & easy.

tiff files, compression, colordepth

P: n/a

I'm having troubles with saving a tiff-file with a certain compression
and colordepth. This is the code I use:

private sub MakeTiff

dim imgSource as new bitmap(strTifSourceFile)

Dim TiffCodecInfo As System.Drawing.Imaging.ImageCodecInfo
Dim CompressionEncoder As System.Drawing.Imaging.Encoder
Dim Params As System.Drawing.Imaging.EncoderParameters
Dim myencoder As System.Drawing.Imaging.Encoder
myencoder = System.Drawing.Imaging.Encoder.ColorDepth
Dim myImageCodecInfo As System.Drawing.Imaging.ImageCodecInfo
myImageCodecInfo = GetEncoderInfo

Dim format As System.Drawing.Imaging.ImageFormat
Dim Index As Integer

'Get the CodecInfo for TIFF format
TiffCodecInfo = GetEncoderInfo

Dim myEncoderParameterColor As New
System.Drawing.Imaging.EncoderParameter(myencoder, 24L)

CompressionEncoder = System.Drawing.Imaging.Encoder.Compression
Dim myEncoderParameterCompression = New
System.Drawing.Imaging.EncoderParameter(Compressio nEncoder, CLng
(System.Drawing.Imaging.EncoderValue.CompressionLZ W))

'Create an EncoderParameters object.
Params = New System.Drawing.Imaging.EncoderParameters(2)
Params.Param(0) = myEncoderParameterCompression
Params.Param(1) = myEncoderParameterColor

imgSource.Save("c:\test.tif", TiffCodecInfo, Params)
end sub
Private Function GetEncoderInfo(ByVal format As
System.Drawing.Imaging.ImageFormat) As

Dim index As Integer
Dim encoders() As System.Drawing.Imaging.ImageCodecInfo
encoders = System.Drawing.Imaging.ImageCodecInfo.GetImageEnco ders()

For index = 0 To (encoders.Length - 1)
If UCase(encoders(index).FormatDescription) = UCase
(format.ToString) Then
Return encoders(index)
End If
Next index
End Function


I can set the colordepth in this part:
Dim myEncoderParameterColor As New
System.Drawing.Imaging.EncoderParameter(myencoder, 24L)
I can set the compression in this part:
CompressionEncoder = System.Drawing.Imaging.Encoder.Compression
Dim myEncoderParameterCompression = New
System.Drawing.Imaging.EncoderParameter(Compressio nEncoder, CLng
(System.Drawing.Imaging.EncoderValue.CompressionLZ W))

The Tif-source file is a 475kb file 200dpi, black and white. I want to
save a rotated version of this tif-file, same size, same dpi, same
colors. I can;t get it done.

(1) I can't set the colordepth to less then 24L. When I do this I get an
error in the GDI+. I would like to have a Tif-file with a colordepth
of 1 bit. Is this possible?

(2) When setting the compression to CompressionLZW or CompressionNone it
works fine. But I can;t select the other compressions. When I do so I
also get an error.

Met vriendelijke groet / With regards / Saludos,
Moviat Automatisering
Maurice Mertens

tel: +31 162 470 534
fax: +31 162 470 502
Jul 21 '05 #1
Share this Question
Share on Google+
1 Reply

P: n/a
Yeah, 1bpp tiffs are annoying and so far as I've found, the managed way is
great for slicing out tif frames but doesn't cut it completely on depth

I got this code from a great newsgroup guy named "BMan" about a year ago. I
made adjustments so that I could set a threshold and let the users despeckle
or allow in more "noise" if needed ... at the moment I can't find that
enhanced code (it was for a previous client). See if the following exact
conversion code helps and over the weekend I'll see if I can track down the
other files.

If you really need CCIT4 compression, as most folks are doing fax image work
with 1bpp tifs, you can change the LZW to CCTI4 and it still works fine.

1) make a class, call it Win32API and paste in the following:

Imports System.Runtime.InteropServices

Public Class win32api

<DllImport("KERNEL32.DLL", EntryPoint:="RtlMoveMemory", _
SetLastError:=True, CharSet:=CharSet.Auto, _
ExactSpelling:=True, _
CallingConvention:=CallingConvention.StdCall)> _

Public Shared Sub CopyArrayTo(<[In](), MarshalAs(UnmanagedType.I4)> ByVal
hpvDest As Int32, <[In](), Out()> ByVal hpvSource() As Byte, ByVal cbCopy As

' Leave function empty

End Sub

' GDI32 Constant Definitions

Public Const BI_RGB = 0&
Public Const DIB_PAL_COLORS = 1

' GDI32 Structure Definitions'

<StructLayout(LayoutKind.Sequential)> Public Structure GDI32BITMAP
Public bmType As Integer
Public bmWidth As Integer
Public bmHeight As Integer
Public bmWidthBytes As Integer
Public bmPlanes As Short
Public bmBitsPixel As Short
Public bmBits As Integer
End Structure

<StructLayout(LayoutKind.Sequential)> Public Structure GDI32BITMAPINFOHEADER
Public biSize As Integer
Public biWidth As Integer
Public biHeight As Integer
Public biPlanes As Short
Public biBitCount As Short
Public biCompression As Integer
Public biSizeImage As Integer
Public biXPelsPerMeter As Integer
Public biYPelsPerMeter As Integer
Public biClrUsed As Integer
Public biClrImportant As Integer
End Structure

' GDI32 DLL Entry Point Declares

Public Declare Function CreateCompatibleDC Lib "gdi32" Alias _
"CreateCompatibleDC" (ByVal hdc As Integer) As Integer

Public Declare Function SelectObject Lib "gdi32" Alias "SelectObject" _
(ByVal hdc As Integer, ByVal hObject As Integer) As Integer

Public Declare Function GetObject Lib "gdi32" Alias "GetObjectA" _
(ByVal hObject As Integer, ByVal nCount As Integer, ByVal lpObject As
Integer) As Integer

Public Declare Function CreateDIBSection Lib "gdi32" Alias _
"CreateDIBSection" (ByVal hDC As Integer, ByRef pBitmapInfo As _

GDI32BITMAPINFOHEADER, ByVal un As Integer, ByRef lplpVoid As Integer, _
ByVal handle As Integer, ByVal dw As Integer) As Integer

Public Declare Function GetDIBits Lib "gdi32" (ByVal aHDC As Integer, _
ByVal hBitmap As Integer, ByVal nStartScan As Integer, ByVal nNumScans _
As Integer, ByVal lpBits As Integer, ByRef lpBI As _
GDI32BITMAPINFOHEADER, ByVal wUsage As Integer) As Integer

Public Declare Function DeleteObject Lib "gdi32" Alias "DeleteObject" _
(ByVal hObject As Integer) As Integer

Public Declare Function DeleteDC Lib "gdi32" Alias "DeleteDC" (ByVal hdc As
Integer) As Integer

Public Declare Function GdiFlush Lib "gdi32" Alias "GdiFlush" () As Integer

End Class

2) make a class, call it TifConverter and paste in this code:

Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO
Imports System.Runtime.InteropServices

Public Class TiffConverter

Public Function MakeCCIT4Exact(ByVal value As Image) As Image
'bman code!
Dim dotNetBitmap As Bitmap
Dim XDPI, YDPI As Single
Dim srcBitmapInfo As win32api.GDI32BITMAP
Dim hSrcBitmap As IntPtr = IntPtr.Zero
Dim pSrcBitmapInfo As IntPtr = IntPtr.Zero
Dim hDstMemDC As IntPtr = IntPtr.op_Explicit(win32api.CreateCompatibleDC(0))
Dim hDestDIBitmap As IntPtr = IntPtr.Zero
Dim hDstOldBitmap As IntPtr = IntPtr.Zero
Dim pDestDIBits As Integer = 0
Dim RCode As Integer
Dim UseCompression As Boolean = True

' Load our Image into our GDI+ bitmap object (1BPP)
dotNetBitmap = CType(value, Bitmap)

XDPI = dotNetBitmap.HorizontalResolution
YDPI = dotNetBitmap.VerticalResolution

Dim xrez As Single = 0
Dim rezratio As Single = 1

xrez = XDPI
rezratio = 1
ElseIf XDPI > YDPI Then
xrez = XDPI
rezratio = XDPI / YDPI
xrez = YDPI
rezratio = XDPI / YDPI
End If

dim b As New Bitmap(dotNetBitmap, New Size(dotNetBitmap.Width,
dotNetBitmap.Height * rezratio))
dotNetBitmap = b

hSrcBitmap = dotNetBitmap.GetHbitmap() ' hSrcBitmap is now 32bpp

' thanks to GDI+
' There most likey should be some protection afforded to the
' following()
' set of statements to ensure the memory operations don't cause a
' GPF in the app

' Allocate enough memory to hold a GDI32 structure and use that as a
' buffer area for the call to GetObject

pSrcBitmapInfo = Marshal.AllocCoTaskMem(Marshal.SizeOf(srcBitmapInf o))

' Call GetObject and use out buffer we just allocated as to store
' the returned struct

win32api.GetObject(hSrcBitmap.ToInt32, Marshal.SizeOf(srcBitmapInfo), _

' Marshall the data in our buffer back to our GDI32BITMAP structure

srcBitmapInfo = CType(Marshal.PtrToStructure(pSrcBitmapInfo, _
GetType(win32api.GDI32BITMAP)), win32api.GDI32BITMAP)

' Release the memory we allocated for the buffer to the GetObject
' Call

If IntPtr.op_Inequality(pSrcBitmapInfo, IntPtr.Zero) Then _

' Now we have the dimensions of the HBITMAP underlying our dot net
' Bitmap Object and can use them to set up our DIB

' BTW, if you don't believe me, look at srcBitmapInfo in the
' debugger and you'll see out image is now 32bpp

.biSize = Marshal.SizeOf(DestDIBMIH)
.biWidth = srcBitmapInfo.bmWidth
.biHeight = srcBitmapInfo.bmHeight 'CInt(srcBitmapInfo.bmHeight * (XDPI /
.biPlanes = srcBitmapInfo.bmPlanes
.biBitCount = srcBitmapInfo.bmPlanes ' 1 bpp * srcBitmapInfo.bmPlanes()
.biCompression = win32api.BI_RGB
End With

' Create our DIBitmap
hDestDIBitmap = _
IntPtr.op_Explicit(win32api.CreateDIBSection(hDstM emDC.ToInt32, DestDIBMIH,
0, _
pDestDIBits, 0, 0))

' Now Select our DIBitmap into its DC
hDstOldBitmap = IntPtr.op_Explicit(win32api.SelectObject(hDstMemDC .ToInt32,

' Copy the bits from out dotNet Bitmaps objects HBITMAP to out
' DIBitmap.

' ** I don't check in this, because I know there's no way it
' could(be)

RCode = win32api.GetDIBits(hDstMemDC.ToInt32, hSrcBitmap.ToInt32, 0, _
srcBitmapInfo.bmHeight, pDestDIBits, DestDIBMIH, win32api.DIB_PAL_COLORS)

' Wait for GDI to finish creating and drawing out DIBitmap

' All Done with the conversion, create a new Bitmap using our DIBitmap()
dotNetBitmap = Bitmap.FromHbitmap(hDestDIBitmap)

' Just because I'm being a pain, make the dpi of the new bitmap
' This might matter to some image viewers

dotNetBitmap.SetResolution(xrez, xrez)

' And finally save it before we cleanup and leave. Should be 1Bpp,
'UseCompression flag determines if compression is to be used

Dim m As New System.IO.MemoryStream
If UseCompression Then
Dim CodecInfo As Imaging.ImageCodecInfo = _
Dim ImgEncoder As New Imaging.Encoder(CodecInfo.Clsid)
Dim EncParms As New Imaging.EncoderParameters(1)
EncParms.Param(0) = New _
Imaging.EncoderParameter(Imaging.Encoder.Compressi on, _

dotNetBitmap.Save(m, CodecInfo, EncParms)
Return Image.FromStream(m)
dotNetBitmap.Save(m, Imaging.ImageFormat.Tiff)
Return Image.FromStream(m)
End If


' Clean up GDI resources before leaving


If IntPtr.op_Inequality(hSrcBitmap, IntPtr.Zero) Then _

If IntPtr.op_Inequality(hDestDIBitmap, IntPtr.Zero) Then _
win32api.DeleteObject(win32api.SelectObject(hDstMe mDC.ToInt32,

If IntPtr.op_Inequality(hDstMemDC, IntPtr.Zero) Then _

End Try

End Function

Private Function GetEncoderInfo(ByVal mimeType As String) As
Dim j As Integer
Dim encoders As Drawing.Imaging.ImageCodecInfo()

encoders = Drawing.Imaging.ImageCodecInfo.GetImageEncoders()

For j = 0 To encoders.Length
If encoders(j).MimeType = mimeType Then
Return encoders(j)
End If

Return Nothing

End Function

End Class

3) make a form, add two buttons and a picturebox. Use one button to load an
image (a jpg or bmp) into the picturebox. Make the other buttom do this:

Dim o As New TiffConverter
Dim i As Image = o.MakeCCIT4Exact(PictureBox1.Image)

Hope it helps

Robert Smith
Kirkland, WA

"Maurice Mertens" <hm*****@nospam.nospam> wrote in message
news:Xn****************************@ 3...

I'm having troubles with saving a tiff-file with a certain compression
and colordepth. This is the code I use:

Jul 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.