Solved

convert .tif to jpg

Posted on 2011-02-25
23
601 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
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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

803 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