 |
|
|
Hi,
I have an image which is a negative TIFF image. When i am trying to save the image, I want to convert it to a normal TIFF image. Can you please tell me how to differentiate between a normal TIFF and a Negative TIFF. I am using C#. Can you please send me the code for checking this. I think we have to change somehting in convertToBitonal(Bitmap original)???
.. I have been trying to find it for a long time.. but in vain... Please help me ASAP.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
First I'd like to say Thanks for the great article/code!
My question is if there is a way to specify the DPI of the resulting tiffs? I have tiffs with 300 DPI B/W and they are ending up as 96 DPI.
Thanks
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Thanks for your appreciation. To answer your question, You are getting resulting image with 96DPI but you want it to be 300DIP. I am not able to understand why it is reducing DPI 96DIP from 300DPI. Can you please try to print your origional image's DPI value before applying any compression. I have the doubts that your image itself is having 96DPI. I dont know about a way to set DPI for an image but if i get something, i will surely post it. Thanks Bijul Soni
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
I'm having the same issue, the resolution is staying the same but the DPI drops from original 200DPI to 96DPI.
Something weird, is that it appears the first page is what is messed up, and not the second in a multi frame tif.
There isn't any data loss, the viewer just shows the tiff first page, 96 points to an inch verses the 200 points to an inch.
Please let me know if you find anything, I'm still looking myself.
Excellent Artcle- Thanks, Jonathan
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
In the ConvertToBitonal routine add the line marked below:
// Create destination bitmap Bitmap destination = new Bitmap(source.Width, source.Height, PixelFormat.Format1bppIndexed); -----> destination.SetResolution(source.HorizontalResolution, source.VerticalResolution);
|
| Sign In·View Thread·PermaLink | 5.00/5 (2 votes) |
|
|
|
 |
|
|
This really helps a lot. Please be aware though that MS fax printer driver seems to have a problem with CCITT4 encoding at 1728x2200, but not with a smaller size (fatal error even though submitted to the fax server successfully). A little bizarre, but the method of solving this is to use no compression.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Works perfectly for what I was trying to do. I had something similar to this, but not laid out as well and it was incredibly slow. Very efficient.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
I do have a question. If the original image is in CCITT4 compression and 1bpp, is there a way to break the image up without having to do the bi-tonal conversion? Everything I've tried looses the compression and it seems there should be a way to take them apart and recombine without loosing the page parameters.
Edit: I see gcorgnet is having the same issue as me, please let us know if you come up with a solution to this problem. Thank you!
-- modified at 9:35 Tuesday 14th August, 2007
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I found a solution and I wanted to share. Instead of passing in an array of bitmaps, I'm passing in an array of Images. For some reason (that I haven't researched), the Image.Save and Image.SaveAdd manages to keep the compression of the original tif. My guess is when converting from Image to Bitmap the compression is lost.
I'm going to try to share my code, if it's not clear let me know and if you want to add this to your article go ahead, I know I've spent a lot of time trying to figure this out so if this helps someone else I'm glad to be of service. 
First you have to break the image up:
private void SplitTiff() { // Create a new image object based on the original tiff imageFile = Image.FromFile("Path to tif here",true);
// Get the dimension properties of the tiff frameDimensions = new FrameDimension(imageFile.FrameDimensionsList[0]);
// Get the number of images in the tiff numberOfImages = imageFile.GetFrameCount(frameDimensions);
// Size the image array img = new Image[numberOfImages];
// Split up the tiff into it's images try { for (int intFrame = 0;intFrame < numberOfImages;intFrame++) { imageFile.SelectActiveFrame(frameDimensions,intFrame); img[intFrame] = imageFile; } } catch (Exception _err) { // TODO: Handle exception } }
Then you call saveMultipageImage passing in the image array, location of the file to be saved, and the type ("TIFF").
public bool saveMultipageImage(Image[] img, string location, string type) { if (img != null) { try { ImageCodecInfo codecInfo = getCodecForstring(type);
if (img.Length == 1) {
EncoderParameters iparams = new EncoderParameters(2); Encoder iparam = Encoder.SaveFlag; EncoderParameter iparamPara = new EncoderParameter(iparam,(long)(EncoderValue.MultiFrame)); iparams.Param[0] = iparamPara; iparam = Encoder.Compression; iparamPara = new EncoderParameter(iparam, (long)(EncoderValue.CompressionCCITT4)); iparams.Param[1] = iparamPara; img[0].Save(location, codecInfo, iparams); } else if (img.Length > 1) {
Encoder saveEncoder; Encoder compressionEncoder; EncoderParameter SaveEncodeParam; EncoderParameter CompressionEncodeParam; EncoderParameters EncoderParams = new EncoderParameters(2);
saveEncoder = Encoder.SaveFlag; compressionEncoder = Encoder.Compression;
// Save the first page (frame). SaveEncodeParam = new EncoderParameter(saveEncoder, (long)EncoderValue.MultiFrame); CompressionEncodeParam = new EncoderParameter(compressionEncoder, (long)EncoderValue.CompressionCCITT4); EncoderParams.Param[0] = CompressionEncodeParam; EncoderParams.Param[1] = SaveEncodeParam;
File.Delete(location); img[0].Save(location, codecInfo, EncoderParams);
for (int i = 1; i < img.Length; i++) { if (img[i] == null) break;
SaveEncodeParam = new EncoderParameter(saveEncoder, (long)EncoderValue.FrameDimensionPage); CompressionEncodeParam = new EncoderParameter(compressionEncoder, (long)EncoderValue.CompressionCCITT4); EncoderParams.Param[0] = CompressionEncodeParam; EncoderParams.Param[1] = SaveEncodeParam; img[0].SaveAdd(img[i], EncoderParams);
}
SaveEncodeParam = new EncoderParameter(saveEncoder, (long)EncoderValue.Flush); EncoderParams.Param[0] = SaveEncodeParam; img[0].SaveAdd(EncoderParams); img[0].Dispose(); } return true; } catch (Exception ee) { img[0].Dispose(); throw new Exception(ee.Message + " Error in saving as multipage "); return false; } } else return false; }
-- modified at 11:02 Tuesday 14th August, 2007
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
|
I'm having problems disconnecting the image from the FileStream. I can store the tiff frames into a file no problem, but when I load it I have a problem having a "stand-alone" copy of the image.
Here's some code:
FileStream fs = File.Open(path, FileMode.Open); Image img = Image.FromStream(fs); pbox.Image = (Image)img.Clone(); //fs.Close();
When I have the above and change the page using img.SelectActiveFrame I have no problem but when I uncomment fs.Close(); I get a GDI+ generic error. This makes me think that the image is still connected to the stream. I tried to avoid this by making a clone of it, but the clone apparently is also attached to the FileStream. Any help would be appreciated.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
hi i also faced the same problem. The reason is if you close the filestream and then call selectframe you wont be able to iterate through pages of your multipage tiff file which is stored on the file system.So you have to put your filestream object open to perform operations like selectFrame. So if possible call fs.close() at the last once all the operations are done.
second thing if your intention is to append already saved multipage tiff file,pleaserefer my tricky code specifically the function saveToExistingFile.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
You'd probably be ok if you Dispose of your image first then close the Stream. Remember that everything in .NET should be disposed of in a proper sequence, usually in the opposite order of which you've created them.
FileStream fs = File.Open(path, FileMode.Open); Image img = Image.FromStream(fs); pbox.Image = (Image)img.Clone(); img.Dispose(); fs.Close();
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi,
I have a bunch of System.Drawing.Image objects that are already bitonal, using PixelFormat.Format1bppIndexed and having a resolution of 200dpi (all of them)
I just want to save them into one unique multipage tiff file.
When I try Image.Save(...) I receive a (very nice) exception : A generic error occured in GDI+...
if I create a new System.Drawing.Bitmap object from the image, the saving actually works but the files is too big (several MB). The cause is that the bitmap that has been created has a 32bpp color depth.
I had a look at your code, that actually works with my images, but I am damn sure there must be a way to avoid all that processing on my images that are already bitonal, etc...
Do you have any idea ?
Thanks,
Guillaume
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Good You have explained your problem in a very nice way.I like that.
Now
As you are saying you are sure that all of your images are bitonal(1bppIndexed),it should work properly for Image.Save method.But as your reference variable is Image object, it is likely that it may convert your bitonal images into 32bpp which was the case while i was working on this code.Therefore it is advisable to convert them into bitonal because sometimes when you put bitonal images into Image/Bitmap object .NET converting them into 32bpp images and that's why you are seeing Generic Exception,
so as per my knowledge only way to tackle this problem is to explicitly convert images into Bitonal.
Still i will try my best to give you proper solution but it will take some time,so currently you can continue with this code
Ok Bye Take Care
|
| Sign In·View Thread·PermaLink | 5.00/5 (3 votes) |
|
|
|
 |
|
|
 |
|
|
Images are converted into ccitt4 format. but what about color/gray/jpg images ? I have *.tif and *.jpg files in the same directory. Now i want to make a multifpage tif image with the same compression. for example if *.tiff files compression is ccitt4 and *.jpg is jpeg then the output file.. which would be multipage tiff, should remain the same compression. Pls help me, help help help help thanks
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
Great work -
I'm having problem when convert C# code to vb.net 2005. I'm getting "Arithmetic operation result in an overflow" exception? I get the image to load but I keep get the exception error when saving it to multipage tiff. am I doing something wrong??
|
| Sign In·View Thread·PermaLink | 2.00/5 (2 votes) |
|
|
|
 |
|
|
can u tell me in which line of code you are having expection ? means either converting image to bitonal image or calling save/save add function or during setting other parameters of image ?
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
The problem occur when calling savemultipage(). Thank you
Public Function saveMultipage(ByVal bmp As Image(), ByVal location As String, ByVal type As String) As Boolean If Not (bmp Is Nothing) Then Try Dim codecInfo As ImageCodecInfo = getCodecForstring(type) Dim i As Integer = 0 While i < bmp.Length If bmp(i) Is Nothing Then ' break End If bmp(i) = CType(ConvertToBitonal(CType(bmp(i), Bitmap)), Image) System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1) End While If bmp.Length = 1 Then Dim iparams As EncoderParameters = New EncoderParameters(1) Dim iparam As Encoder = Encoder.Compression Dim iparamPara As EncoderParameter = New EncoderParameter(iparam, CType((EncoderValue.CompressionCCITT4), Long)) iparams.Param(0) = iparamPara bmp(0).Save(location, codecInfo, iparams) Else If bmp.Length > 1 Then Dim saveEncoder As Encoder Dim compressionEncoder As Encoder Dim SaveEncodeParam As EncoderParameter Dim CompressionEncodeParam As EncoderParameter Dim EncoderParams As EncoderParameters = New EncoderParameters(2) saveEncoder = Encoder.SaveFlag compressionEncoder = Encoder.Compression SaveEncodeParam = New EncoderParameter(saveEncoder, CType(EncoderValue.MultiFrame, Long)) CompressionEncodeParam = New EncoderParameter(compressionEncoder, CType(EncoderValue.CompressionCCITT4, Long)) EncoderParams.Param(0) = CompressionEncodeParam EncoderParams.Param(1) = SaveEncodeParam File.Delete(location) bmp(0).Save(location, codecInfo, EncoderParams) i = 1 While i < bmp.Length If bmp(i) Is Nothing Then Exit While End If SaveEncodeParam = New EncoderParameter(saveEncoder, CType(EncoderValue.FrameDimensionPage, Long)) CompressionEncodeParam = New EncoderParameter(compressionEncoder, CType(EncoderValue.CompressionCCITT4, Long)) EncoderParams.Param(0) = CompressionEncodeParam EncoderParams.Param(1) = SaveEncodeParam bmp(0).SaveAdd(bmp(i), EncoderParams) System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1) End While SaveEncodeParam = New EncoderParameter(saveEncoder, CType(EncoderValue.Flush, Long)) EncoderParams.Param(0) = SaveEncodeParam bmp(0).SaveAdd(EncoderParams) End If End If Return True Catch ee As System.Exception Throw New Exception(ee.Message + " Error in saving as multipage ") End Try Else Return False End If End Function
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Naughty, asking in all the forums! I converted it to vb here: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1360678&SiteID=1&mode=1
The overflow was occurring elsewhere in the autotranslated code, the culprit is fine in the original:
// Compute pixel brightness (i.e. total of Red, Green, and Blue values) pixelTotal = sourceBuffer[sourceIndex + 1] + sourceBuffer[sourceIndex + 2] + sourceBuffer[sourceIndex + 3];
in vb you need to cast all the bytes to int first.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
thanks joOls ... i wasnt check on microsoft forums so i didnt know. thanks for translate it to vb for me. that's big help.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I have a need to directly capture reports being sent from and AS400 as a PCL. I capture this data with Remote Print Manager Software.
I need to then display this report, Add a digital signature, and then save as a TIFF ?
Mike Dibble
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |