473,320 Members | 2,202 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

tiff files, compression, colordepth

Hello,

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
(System.Drawing.Imaging.ImageFormat.Tiff)

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

'Get the CodecInfo for TIFF format
TiffCodecInfo = GetEncoderInfo
(System.Drawing.Imaging.ImageFormat.Tiff)

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

'Compression
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
System.Drawing.Imaging.ImageCodecInfo

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:
'Colordepth
Dim myEncoderParameterColor As New
System.Drawing.Imaging.EncoderParameter(myencoder, 24L)
I can set the compression in this part:
'Compression
CompressionEncoder = System.Drawing.Imaging.Encoder.Compression
Dim myEncoderParameterCompression = New
System.Drawing.Imaging.EncoderParameter(Compressio nEncoder, CLng
(System.Drawing.Imaging.EncoderValue.CompressionLZ W))

PROBLEM:
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
mauricem@moviat_KillSpamWordForEMail.nl

tel: +31 162 470 534
fax: +31 162 470 502
Jul 21 '05 #1
1 12250
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
conversions.

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
Integer)

' 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 DestDIBMIH As win32api.GDI32BITMAPINFOHEADER
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

Try
' 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

If XDPI = YDPI Then
xrez = XDPI
rezratio = 1
ElseIf XDPI > YDPI Then
xrez = XDPI
rezratio = XDPI / YDPI
Else
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), _
pSrcBitmapInfo.ToInt32)

' 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 _
Marshal.FreeCoTaskMem(pSrcBitmapInfo)

' 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

With DestDIBMIH
.biSize = Marshal.SizeOf(DestDIBMIH)
.biWidth = srcBitmapInfo.bmWidth
.biHeight = srcBitmapInfo.bmHeight 'CInt(srcBitmapInfo.bmHeight * (XDPI /
YDPI))
.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,
_
hDestDIBitmap.ToInt32))

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

' ** IMPORTANT NOTE **
' ** THE SOURCE HBITMAP CANNOT BE SELECTED INTO ANY DC
' ** 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
win32api.GdiFlush()

' 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 = _
GetEncoderInfo("image/tiff")
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, _
Imaging.EncoderValue.CompressionLZW)

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

Finally

' Clean up GDI resources before leaving

dotNetBitmap.Dispose()

If IntPtr.op_Inequality(hSrcBitmap, IntPtr.Zero) Then _
win32api.DeleteObject(hSrcBitmap.ToInt32)

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

If IntPtr.op_Inequality(hDstMemDC, IntPtr.Zero) Then _
win32api.DeleteDC(hDstMemDC.ToInt32)

End Try

End Function

Private Function GetEncoderInfo(ByVal mimeType As String) As
System.Drawing.Imaging.ImageCodecInfo
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
Next

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)
i.Save("c:\testarea\test.tif")

Hope it helps

Robert Smith
Kirkland, WA
www.smithvoice.com



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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bob | last post by:
I have a query whose results contain the full path (i.e. c:\Images\xxx.tiff) to MUTI-PAGE (All two page) TIFF documents. I need to figure out how to BATCH print the tiff's using the path from...
3
by: J Cristancho | last post by:
I would like to know how to display and manipulate TIFF and multipage TIFF Files in my ASP.NET Applications. I know there's third party controls, but I'd like to do it myself Thanks!!!
1
by: Maurice Mertens | last post by:
Hello, I'm having troubles with saving a tiff-file with a certain compression and colordepth. This is the code I use: ----------------------------------------------------------------------...
4
by: sophie_newbie | last post by:
Hey guys, As part of a college project I'm trying to write a script to convert TIFF images downloaded from the US patent office site, www.uspto.gov. The tiff images are encoded using CCITT...
8
by: sophie_newbie | last post by:
Hey guys, OK this is a long shot but does anyone know of a way to uncompress tiff files directly in python. I know it can be done with an os call but that'll only work on unix and only if the...
0
by: genc ymeri | last post by:
Hi over there, Does the .Net20 framework provide library classes/drivers to convert multiple single tiff files into a single multi-pages file or should I use third party tools ? Genc.
2
by: nanjunda | last post by:
Hi, How to open tiff files in asp.net image to display. And also how to use alternaTiff in asp.net image to display tiff. Thanks in advance. Regards, Nanjunda
2
by: lavadan | last post by:
Hi Folks, I am a newbie to SQL Server. Please help me with this. I have some tiff files. I have to upload them into sql server. How do i do this. I will be getting the tiff files in a hardware...
1
by: David C | last post by:
Is there a way in ASP.Net 2.0 to stream individual TIFF image files into one TIFF file? If so, can you point me to any details? Thank you. David
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.