Solved

convert .tif to jpg

Posted on 2011-02-25
23
607 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 11
23 Comments
 
LVL 23

Expert Comment

by:wdosanjos
ID: 34983699
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
ID: 34983999
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
ID: 34984211
Try the following:

dim img as Image = Image.FromStream(new MemoryStream(imageBytesFromDb))
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Author Comment

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

Expert Comment

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

Author Comment

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

Expert Comment

by:wdosanjos
ID: 34992026
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
ID: 35009198
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
ID: 35010063
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
ID: 35015193
@ 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
ID: 35016014
Do you mean you get an exception on that line?  If yes, what's the exception?
0
 
LVL 16

Author Comment

by:Kamal Khaleefa
ID: 35016288
yes
parameter not valid
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35016350
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
ID: 35016432
tifbytes contain bytes and it not equal to nothing
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35016626
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
ID: 35024320
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
ID: 35029161
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
ID: 35053558
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
ID: 35057594
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
ID: 35067176
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
ID: 35070281
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
ID: 35119902
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
ID: 35718349
no solution were provided
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

691 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