[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 622
  • Last Modified:

convert .tif to jpg

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
Kamal Khaleefa
Asked:
Kamal Khaleefa
  • 12
  • 11
1 Solution
 
wdosanjosCommented:
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
 
Kamal KhaleefaInformation Security SpecialistAuthor Commented:
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
 
wdosanjosCommented:
Try the following:

dim img as Image = Image.FromStream(new MemoryStream(imageBytesFromDb))
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
Kamal KhaleefaInformation Security SpecialistAuthor Commented:
i tried that
and i got the error parameter not valid
0
 
wdosanjosCommented:
What's the type of "imageBytesFromDb" on your code?  It should be byte().
0
 
Kamal KhaleefaInformation Security SpecialistAuthor Commented:
hi
imageBytesFromDb is a function that returns the image from data base
its type is byte()
0
 
wdosanjosCommented:
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
 
Kamal KhaleefaInformation Security SpecialistAuthor Commented:
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
 
wdosanjosCommented:
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
 
Kamal KhaleefaInformation Security SpecialistAuthor Commented:
@ this line
    Dim img As Image = Image.FromStream(New MemoryStream(tifBytes))
i have parameter not valid
image is stored in database as tif
0
 
wdosanjosCommented:
Do you mean you get an exception on that line?  If yes, what's the exception?
0
 
Kamal KhaleefaInformation Security SpecialistAuthor Commented:
yes
parameter not valid
0
 
wdosanjosCommented:
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
 
Kamal KhaleefaInformation Security SpecialistAuthor Commented:
tifbytes contain bytes and it not equal to nothing
0
 
wdosanjosCommented:
Have you checked the saved file? Does it contain a valid image? Does the image display properly?
0
 
Kamal KhaleefaInformation Security SpecialistAuthor Commented:
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
 
wdosanjosCommented:
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
 
Kamal KhaleefaInformation Security SpecialistAuthor Commented:
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
 
wdosanjosCommented:
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
 
Kamal KhaleefaInformation Security SpecialistAuthor Commented:
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
 
wdosanjosCommented:
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
 
Kamal KhaleefaInformation Security SpecialistAuthor Commented:
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
 
Kamal KhaleefaInformation Security SpecialistAuthor Commented:
no solution were provided
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 12
  • 11
Tackle projects and never again get stuck behind a technical roadblock.
Join Now