Solved

convert .tif to jpg

Posted on 2011-02-25
23
594 Views
Last Modified: 2012-05-11
Hi
i have a filed in data base of type image
im storing images of type .tif in it
my problem is that i can not view the tif images in browser so how i can convert these images
from tif to jpg so i can preview them in browser
0
Comment
Question by:Kamal Khaleefa
  • 12
  • 11
23 Comments
 
LVL 23

Expert Comment

by:wdosanjos
Comment Utility
You can use the 'Image' class Image.FromFile() and Image.Save methods.  Save takes the file format as a parameter.

Image class documentation
http://msdn.microsoft.com/en-us/library/system.drawing.image.aspx

I hope this helps.
0
 
LVL 16

Author Comment

by:Kamal Khaleefa
Comment Utility
my image is stored in SQL DB
also when i try to do the following

dim img as system.drawing.image=(image bytes from DB)
it gives me an error
parameter is not valid
Thanks
0
 
LVL 23

Expert Comment

by:wdosanjos
Comment Utility
Try the following:

dim img as Image = Image.FromStream(new MemoryStream(imageBytesFromDb))
0
 
LVL 16

Author Comment

by:Kamal Khaleefa
Comment Utility
i tried that
and i got the error parameter not valid
0
 
LVL 23

Expert Comment

by:wdosanjos
Comment Utility
What's the type of "imageBytesFromDb" on your code?  It should be byte().
0
 
LVL 16

Author Comment

by:Kamal Khaleefa
Comment Utility
hi
imageBytesFromDb is a function that returns the image from data base
its type is byte()
0
 
LVL 23

Expert Comment

by:wdosanjos
Comment Utility
Here is a complete sample.  It reads the tif image into a byte array then saves it to a jpeg file and a jpeg byte array.  You should be able to leverage it to your particular need.

Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO

Module Module1

    Sub Main()
        Dim tifImageInfo As FileInfo = New FileInfo("C:\temp\MyImage.tif")
        Dim tifImageFile As FileStream = New FileStream(tifImageInfo.FullName, FileMode.Open)
        Dim tifImageBytes() As Byte = New Byte(tifImageInfo.Length) {}

        tifImageFile.Read(tifImageBytes, 0, tifImageBytes.Length)
        tifImageFile.Close()

        ' Create Image from tif bytes
        Dim img As Image = Image.FromStream(New MemoryStream(tifImageBytes))

        ' Save to jpeg file
        img.Save("C:\temp\MyImage.jpg", ImageFormat.Jpeg)

        ' Save to jpeg byte array
        Dim jpegStream As MemoryStream = New MemoryStream()

        img.Save(jpegStream, ImageFormat.Jpeg)

        Dim jpegBytes() As Byte = jpegStream.ToArray()

        jpegStream.Close()
    End Sub

End Module

Open in new window


I hope this helps.
0
 
LVL 16

Author Comment

by:Kamal Khaleefa
Comment Utility
hi
my image is stored on the database not on a physical folder
so when i try to read it from database and assign it to a image variable it gives me
parameter not valid
0
 
LVL 23

Expert Comment

by:wdosanjos
Comment Utility
Here is the updated sample getting the image from the db:

Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO
Imports System.Data.SqlClient

Module Module1

    Sub Main()
        Dim tifBytes() As Byte
        Dim jpegBytes() As Byte

        Using cn As New SqlConnection("Data Source=.\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True;MultipleActiveResultSets=True")

            Dim cmd As SqlCommand = New SqlCommand("Select MyImage From MyImages Where Id = 1", cn)

            cn.Open()

            Dim rdr As SqlDataReader = cmd.ExecuteReader()

            If (rdr.Read()) Then
                ' Get tif bytes
                tifBytes = TryCast(rdr("MyImage"), Byte())

                ' Create Image from tif bytes
                Dim img As Image = Image.FromStream(New MemoryStream(tifBytes))

                ' Save to jpeg file
                img.Save("C:\temp\MyImage.jpg", ImageFormat.Jpeg)

                ' Save to jpeg byte array
                Dim jpegStream As MemoryStream = New MemoryStream()

                img.Save(jpegStream, ImageFormat.Jpeg)

                jpegBytes = jpegStream.ToArray()

                jpegStream.Close()
            End If

            rdr.Close()
        End Using
    End Sub

End Module

Open in new window

0
 
LVL 16

Author Comment

by:Kamal Khaleefa
Comment Utility
@ this line
    Dim img As Image = Image.FromStream(New MemoryStream(tifBytes))
i have parameter not valid
image is stored in database as tif
0
 
LVL 23

Expert Comment

by:wdosanjos
Comment Utility
Do you mean you get an exception on that line?  If yes, what's the exception?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 16

Author Comment

by:Kamal Khaleefa
Comment Utility
yes
parameter not valid
0
 
LVL 23

Expert Comment

by:wdosanjos
Comment Utility
Either 'tifBytes' does not contain a valid image format or 'tifBytes' is empty.  

You can save the bytes to a file and then open it to verify.  Please add the following code before the line with the exception.  Run the program again and then check if you can successfully view the image file created, C:\myimage.tif.

        Dim f As FileStream = New FileStream("C:\myimage.tif", FileMode.Create)

        f.Write(tifBytes, 0, tifBytes.Length)

        f.Close()

Open in new window

0
 
LVL 16

Author Comment

by:Kamal Khaleefa
Comment Utility
tifbytes contain bytes and it not equal to nothing
0
 
LVL 23

Expert Comment

by:wdosanjos
Comment Utility
Have you checked the saved file? Does it contain a valid image? Does the image display properly?
0
 
LVL 16

Author Comment

by:Kamal Khaleefa
Comment Utility
yes
if i use the method of
  Response.BinaryWrite(CType(dt.Rows(0)("FileUploaded"), Byte()))

                                                            a pop up will come in firefox to save it
and yes it is a valid image
but in IE the browser will hang and stop working and nothing is come
0
 
LVL 23

Expert Comment

by:wdosanjos
Comment Utility
Can you attach the saved image (from firefox) to this thread?  I tested my sample with TIF images I have it works fine.  I want to try with your image here.
0
 
LVL 16

Author Comment

by:Kamal Khaleefa
Comment Utility
hi
i cant attach the image because it contain sensitive data
these documents (tif) we got them from scanning
we scan papers and the extension which we save with is tif
and it is the only option that is allowed to save with
maybe we are not able to read the bytes in image object because the image contains some properties that are not compatible with this object
any ideas??
0
 
LVL 23

Expert Comment

by:wdosanjos
Comment Utility
The Image class is not recognizing your image as valid for some reason.  You could try to turn off validation and see if that helps.

         Dim img As Image = Image.FromStream(New MemoryStream(tifBytes), true, false)

BTW, you can try the following to work around IE hanging.  Content-Disposition forces the browser to download the resource instead of trying to render it. And you also need to provide the content type.  

     Response.ContentType = "image/tiff"  ' image/jpeg   for JPEGs
     Response.AddHeader("Content-Disposition", "attachment; filename=MyImage.tif")
     Response.BinaryWrite(CType(dt.Rows(0)("FileUploaded"), Byte()))
0
 
LVL 16

Author Comment

by:Kamal Khaleefa
Comment Utility
also this is didnt work
Dim img As Image = Image.FromStream(New MemoryStream(tifBytes), true, false)

i know all these things i need something different
0
 
LVL 23

Expert Comment

by:wdosanjos
Comment Utility
Can you create a sample TIF image that has this issue and attach it to this thread?  

I'm not able to reproduce the issue on my end.  All tif files I have tried converted to jpeg successfully with the code I posted earlier.  If GDI+ (Image class) doesn't recognize the image, then there is something special or wrong about it.  Can you open the tif on 'Windows Picture and Fax Viewer' successfully?  Or, do you need a special program to view these tif's?

I found this post reporting a similar issue, but there is no clear resolution:

http://social.msdn.microsoft.com/Forums/en/netfxbcl/thread/624df111-7e0b-4df1-8bb4-6060366ff51c
0
 
LVL 16

Accepted Solution

by:
Kamal Khaleefa earned 0 total points
Comment Utility
Scan11-03-13-0909.tif

Hi
i have attached the image
do the following
1) read the image bytes without converting it to any type
save the image in database
now try to read the bytes from the database in an image class ans see the error
thanks
0
 
LVL 16

Author Closing Comment

by:Kamal Khaleefa
Comment Utility
no solution were provided
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now