2014-01-15 28 views
0

這是我用來上傳文件並將其保存在文件夾中的代碼,但問題與.tif圖像一樣,因爲瀏覽器不支持它們。
因此,我想在保存到文件夾之前將圖像轉換爲.jpg如何通過在fileupload期間轉換成jpg保存tif格式的圖像?

這是我當前的代碼:

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 

的文件保存到文件夾,我想檢查如果是則先將其轉換爲jpg然後將其保存到文件夾的任何.tif上傳之前。

+0

您的問題使用英文單詞,但似乎並未傳達完整的思想。請再試一次。 –

回答

0

我得到了解決。 我用下面的代碼:

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

這個代碼能夠.TIF爲JPG轉換,但轉換後,我無法顯示圖像文件。

1

發現這個功能在谷歌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 
+0

謝謝你的答案,但你可以給我任何建議,我可以在我上面提到的代碼中使用@ al-3sli – sona

+0

在你的條件後使用它'if hpf.ContentLength> 0 Then'創建一個條件來檢查它是否'tif'或不,如果'tif'然後調用像這樣的'ConvertTiffToJpeg(FileName)' –

+0

感謝您的幫助。我希望它會工作@ al-3sli – sona

相關問題