473,748 Members | 7,217 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(strTifSo urceFile)

Dim TiffCodecInfo As System.Drawing. Imaging.ImageCo decInfo
Dim CompressionEnco der As System.Drawing. Imaging.Encoder
Dim Params As System.Drawing. Imaging.Encoder Parameters
Dim myencoder As System.Drawing. Imaging.Encoder
myencoder = System.Drawing. Imaging.Encoder .ColorDepth
Dim myImageCodecInf o As System.Drawing. Imaging.ImageCo decInfo
myImageCodecInf o = GetEncoderInfo
(System.Drawing .Imaging.ImageF ormat.Tiff)

Dim format As System.Drawing. Imaging.ImageFo rmat
Dim Index As Integer

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

'Colordepth
Dim myEncoderParame terColor As New
System.Drawing. Imaging.Encoder Parameter(myenc oder, 24L)

'Compression
CompressionEnco der = System.Drawing. Imaging.Encoder .Compression
Dim myEncoderParame terCompression = New
System.Drawing. Imaging.Encoder Parameter(Compr essionEncoder, CLng
(System.Drawing .Imaging.Encode rValue.Compress ionLZW))

'Create an EncoderParamete rs object.
Params = New System.Drawing. Imaging.Encoder Parameters(2)
Params.Param(0) = myEncoderParame terCompression
Params.Param(1) = myEncoderParame terColor

imgSource.Save( "c:\test.ti f", TiffCodecInfo, Params)
end sub
Private Function GetEncoderInfo( ByVal format As
System.Drawing. Imaging.ImageFo rmat) As
System.Drawing. Imaging.ImageCo decInfo

Dim index As Integer
Dim encoders() As System.Drawing. Imaging.ImageCo decInfo
encoders = System.Drawing. Imaging.ImageCo decInfo.GetImag eEncoders()

For index = 0 To (encoders.Lengt h - 1)
If UCase(encoders( index).FormatDe scription) = UCase
(format.ToStrin g) Then
Return encoders(index)
End If
Next index
End Function

-----------------------------------------------------------------

I can set the colordepth in this part:
'Colordepth
Dim myEncoderParame terColor As New
System.Drawing. Imaging.Encoder Parameter(myenc oder, 24L)
I can set the compression in this part:
'Compression
CompressionEnco der = System.Drawing. Imaging.Encoder .Compression
Dim myEncoderParame terCompression = New
System.Drawing. Imaging.Encoder Parameter(Compr essionEncoder, CLng
(System.Drawing .Imaging.Encode rValue.Compress ionLZW))

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 _KillSpamWordFo rEMail.nl

tel: +31 162 470 534
fax: +31 162 470 502
Jul 21 '05 #1
1 12306
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("KER NEL32.DLL", EntryPoint:="Rt lMoveMemory", _
SetLastError:=T rue, CharSet:=CharSe t.Auto, _
ExactSpelling:= True, _
CallingConventi on:=CallingConv ention.StdCall) > _

Public Shared Sub CopyArrayTo(<[In](), MarshalAs(Unman agedType.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(L ayoutKind.Seque ntial)> 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(L ayoutKind.Seque ntial)> Public Structure GDI32BITMAPINFO HEADER
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 CreateCompatibl eDC Lib "gdi32" Alias _
"CreateCompatib leDC" (ByVal hdc As Integer) As Integer

Public Declare Function SelectObject Lib "gdi32" Alias "SelectObje ct" _
(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 CreateDIBSectio n Lib "gdi32" Alias _
"CreateDIBSecti on" (ByVal hDC As Integer, ByRef pBitmapInfo As _

GDI32BITMAPINFO HEADER, 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 _
GDI32BITMAPINFO HEADER, ByVal wUsage As Integer) As Integer

Public Declare Function DeleteObject Lib "gdi32" Alias "DeleteObje ct" _
(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.GDI32B ITMAP
Dim hSrcBitmap As IntPtr = IntPtr.Zero
Dim pSrcBitmapInfo As IntPtr = IntPtr.Zero
Dim hDstMemDC As IntPtr = IntPtr.op_Expli cit(win32api.Cr eateCompatibleD C(0))
Dim DestDIBMIH As win32api.GDI32B ITMAPINFOHEADER
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.Ho rizontalResolut ion
YDPI = dotNetBitmap.Ve rticalResolutio n

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(dotNetBi tmap, New Size(dotNetBitm ap.Width,
dotNetBitmap.He ight * rezratio))
dotNetBitmap = b

hSrcBitmap = dotNetBitmap.Ge tHbitmap() ' 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.AllocCo TaskMem(Marshal .SizeOf(srcBitm apInfo))

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

win32api.GetObj ect(hSrcBitmap. ToInt32, Marshal.SizeOf( srcBitmapInfo), _
pSrcBitmapInfo. ToInt32)

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

srcBitmapInfo = CType(Marshal.P trToStructure(p SrcBitmapInfo, _
GetType(win32ap i.GDI32BITMAP)) , win32api.GDI32B ITMAP)

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

If IntPtr.op_Inequ ality(pSrcBitma pInfo, IntPtr.Zero) Then _
Marshal.FreeCoT askMem(pSrcBitm apInfo)

' 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.b mWidth
.biHeight = srcBitmapInfo.b mHeight 'CInt(srcBitmap Info.bmHeight * (XDPI /
YDPI))
.biPlanes = srcBitmapInfo.b mPlanes
.biBitCount = srcBitmapInfo.b mPlanes ' 1 bpp * srcBitmapInfo.b mPlanes()
.biCompression = win32api.BI_RGB
End With

' Create our DIBitmap
hDestDIBitmap = _
IntPtr.op_Expli cit(win32api.Cr eateDIBSection( hDstMemDC.ToInt 32, DestDIBMIH,
0, _
pDestDIBits, 0, 0))

' Now Select our DIBitmap into its DC
hDstOldBitmap = IntPtr.op_Expli cit(win32api.Se lectObject(hDst MemDC.ToInt32,
_
hDestDIBitmap.T oInt32))

' 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.GetDIB its(hDstMemDC.T oInt32, hSrcBitmap.ToIn t32, 0, _
srcBitmapInfo.b mHeight, pDestDIBits, DestDIBMIH, win32api.DIB_PA L_COLORS)

' Wait for GDI to finish creating and drawing out DIBitmap
win32api.GdiFlu sh()

' All Done with the conversion, create a new Bitmap using our DIBitmap()
dotNetBitmap = Bitmap.FromHbit map(hDestDIBitm ap)

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

dotNetBitmap.Se tResolution(xre z, 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.Memor yStream
If UseCompression Then
Dim CodecInfo As Imaging.ImageCo decInfo = _
GetEncoderInfo( "image/tiff")
Dim ImgEncoder As New Imaging.Encoder (CodecInfo.Clsi d)
Dim EncParms As New Imaging.Encoder Parameters(1)
EncParms.Param( 0) = New _
Imaging.Encoder Parameter(Imagi ng.Encoder.Comp ression, _
Imaging.Encoder Value.Compressi onLZW)

dotNetBitmap.Sa ve(m, CodecInfo, EncParms)
Return Image.FromStrea m(m)
Else
dotNetBitmap.Sa ve(m, Imaging.ImageFo rmat.Tiff)
Return Image.FromStrea m(m)
End If

Finally

' Clean up GDI resources before leaving

dotNetBitmap.Di spose()

If IntPtr.op_Inequ ality(hSrcBitma p, IntPtr.Zero) Then _
win32api.Delete Object(hSrcBitm ap.ToInt32)

If IntPtr.op_Inequ ality(hDestDIBi tmap, IntPtr.Zero) Then _
win32api.Delete Object(win32api .SelectObject(h DstMemDC.ToInt3 2,
hDstOldBitmap.T oInt32))

If IntPtr.op_Inequ ality(hDstMemDC , IntPtr.Zero) Then _
win32api.Delete DC(hDstMemDC.To Int32)

End Try

End Function

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

encoders = Drawing.Imaging .ImageCodecInfo .GetImageEncode rs()

For j = 0 To encoders.Length
If encoders(j).Mim eType = 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.MakeCCIT4Exac t(PictureBox1.I mage)
i.Save("c:\test area\test.tif")

Hope it helps

Robert Smith
Kirkland, WA
www.smithvoice.com



"Maurice Mertens" <hm*****@nospam .nospam> wrote in message
news:Xn******** *************** *****@194.109.1 33.133...
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
3707
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 the query results. Currently, I have to select each record individually on a continuous form (driven by the query results) and click a cmd button to get the image to open in the windows registered app and print from there. This works fine if...
3
2048
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
388
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: ---------------------------------------------------------------------- private sub MakeTiff dim imgSource as new bitmap(strTifSourceFile)
4
8042
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 Group 4 compression and appear to throw an error when i try to save them using the Image library: >>> im.save(outfile, "PDF)
8
3112
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 right software is installed! I need to convert tiff images downloaded from uspto.gov to pdf, these images are compressed using G4, so I dunno if anyone knows of a method of doing this witout the os call! Surely there is a utility out there...
0
1485
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
15374
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
2042
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 and from there i have to load these images into sql server. Is it a good idea to load the entire images into the database or just the pointers to the images. My manager said that they want to load the images into respective folders. But i think...
1
1230
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
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8830
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9324
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9247
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8243
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4606
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3313
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.