sona
sona

Reputation: 1552

how to save an image of tif format by converting into jpg during fileupload?

This is the code I have used to upload a file and save it in a folder, but the problem is with .tif images, as browsers does not support them.
Because of this, I want to convert the image to .jpg before saving to folder.

This is my current code:

 Try
        Dim j As Integer = 0
        Dim hfc As HttpFileCollection = Request.Files
        Dim PathName As String
        For i As Integer = 0 To hfc.Count - 1
            Dim hpf As HttpPostedFile = hfc(i)

            If hpf.ContentLength > 0 Then

                  PathName = System.IO.Path.GetFileName(hpf.FileName)
                Dim QuoteNumber As String = objQuoteParent.QuoteTabImage.ToList().Item(i).QuoteNumber
                Dim FName As String = QuoteNumber + "_QOT_" + (i.ToString()) + PathName
                Dim FileName As String = "~/UploadedImages/" + FName

               'what should i use before saving to convert a tif file to jpg then save?

                hpf.SaveAs(Server.MapPath("~/UploadedImages\") & FName)

            End If
        Next
    Catch generatedExceptionName As Exception

        Throw
    End Try

before saving the file into folder I want to check if any .tif is uploaded if yes then first convert it to jpg then save it into the folder.

Upvotes: 0

Views: 618

Answers (2)

sona
sona

Reputation: 1552

I got the solution. I used the below code:

   My.Computer.FileSystem.RenameFile(filepath, Name + ".jpg")

This code is able to convert .tif to jpg but after converting i am unable to display the image file.

Upvotes: 0

Al-3sli
Al-3sli

Reputation: 2181

Found This Function on Google TIFF image <--> JPEG image converter

Public Shared Function ConvertTiffToJpeg(fileName As String) As String()
    Using imageFile As Image = Image.FromFile(fileName)
        Dim frameDimensions As New FrameDimension(imageFile.FrameDimensionsList(0))

        ' Gets the number of pages from the tiff image (if multi-page)
        Dim frameNum As Integer = imageFile.GetFrameCount(frameDimensions)
        Dim jpegPaths As String() = New String(frameNum) {}

        Dim frame As Integer = 0
        While frame < frameNum
            ' Selects one frame at a time and save as jpeg.
            imageFile.SelectActiveFrame(frameDimensions, frame)
            Using bmp As New Bitmap(imageFile)
                jpegPaths(frame) = [String].Format("{0}\{1}{2}.jpeg",
                                   Path.GetDirectoryName(fileName),
                                   Path.GetFileNameWithoutExtension(fileName), frame)
                bmp.Save(jpegPaths(frame), ImageFormat.Jpeg)
            End Using
            System.Math.Max(System.Threading.Interlocked.Increment(frame), frame - 1)
        End While

        Return jpegPaths
    End Using
End Function

Upvotes: 1

Related Questions