Solved

Convert TIFF to JPEG

Posted on 2004-08-06
10
6,007 Views
Last Modified: 2008-01-09
Is there any way with VB.NET I can convert a folder of tiff images to jpegs?
0
Comment
Question by:PLavelle
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 4

Expert Comment

by:srcalc
ID: 11739857
Perhaps look at:
http://imagemagick.sourceforge.net/

I know that this one is in VB6 but you can open the .frm in notepad and see what it is doing:
http://www.a1vbcode.com/app.asp?ID=1484

This is probably your best bet though, I tested the program and it works perfectly for me:
http://www.visual-basic-data-mining.net/Forum/ShowPost.aspx?PostID=672
0
 
LVL 17

Accepted Solution

by:
AerosSaga earned 500 total points
ID: 11740091
How bout reversing the flow of this code which shows you how to do bmp to whatever conversions, should be easy to modify for whatever your needs are.

Imports System.IO.Path
Imports System.Drawing.Imaging

  Public Function ConvertBMP(ByVal BMPFullPath As String, _
    ByVal imgFormat As ImageFormat) As Boolean

        Dim bAns As Boolean
        Dim sNewFile As String

        Try
            'bitmap class in system.drawing.imaging
            Dim objBmp As New Bitmap(BMPFullPath)

            'below 2 functions in system.io.path
            sNewFile = GetDirectoryName(BMPFullPath)
            sNewFile &= GetFileNameWithoutExtension(BMPFullPath)

            sNewFile &= "." & imgFormat.ToString
            objBmp.Save(sNewFile, imgFormat)

            bAns = True 'return true on success
        Catch
            bAns = False 'return false on error
        End Try
        Return bAns

    End Function
    'USAGE
    'ConvertBMP("C:\test.bmp", ImageFormat.Jpeg)
    'ConvertBMP("C:\test.bmp", ImageFormat.Emf)
    'ConvertBMP("C:\test.bmp", ImageFormat.Exif)
    'ConvertBMP("C:\test.bmp", ImageFormat.Gif)
    'ConvertBMP("C:\test.bmp", ImageFormat.Icon)
    'ConvertBMP("C:\test.bmp", ImageFormat.MemoryBmp)
    'ConvertBMP("C:\test.bmp", ImageFormat.Png)
    'ConvertBMP("C:\test.bmp", ImageFormat.Tiff)
    'ConvertBMP("C:\test.bmp", ImageFormat.Wmf)

Regards,

Aeros
0
 
LVL 11

Expert Comment

by:ajaikumarr
ID: 11741906
Hai,

Check this code

Imports System.IO.Path
Imports System.Drawing.Imaging

Public Class _TestingForm
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Private WithEvents txtFolderLocation As System.Windows.Forms.TextBox
    Private WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents chkDeleteSource As System.Windows.Forms.CheckBox
    Private WithEvents btnBrowse As System.Windows.Forms.Button
    Friend WithEvents lblStatus As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.txtFolderLocation = New System.Windows.Forms.TextBox
        Me.Button1 = New System.Windows.Forms.Button
        Me.chkDeleteSource = New System.Windows.Forms.CheckBox
        Me.btnBrowse = New System.Windows.Forms.Button
        Me.lblStatus = New System.Windows.Forms.Label
        Me.SuspendLayout()
        '
        'txtFolderLocation
        '
        Me.txtFolderLocation.Location = New System.Drawing.Point(8, 8)
        Me.txtFolderLocation.Name = "txtFolderLocation"
        Me.txtFolderLocation.ReadOnly = True
        Me.txtFolderLocation.Size = New System.Drawing.Size(320, 20)
        Me.txtFolderLocation.TabIndex = 0
        Me.txtFolderLocation.Text = ""
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(344, 40)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Convert"
        '
        'chkDeleteSource
        '
        Me.chkDeleteSource.Checked = True
        Me.chkDeleteSource.CheckState = System.Windows.Forms.CheckState.Checked
        Me.chkDeleteSource.Location = New System.Drawing.Point(8, 40)
        Me.chkDeleteSource.Name = "chkDeleteSource"
        Me.chkDeleteSource.Size = New System.Drawing.Size(320, 24)
        Me.chkDeleteSource.TabIndex = 2
        Me.chkDeleteSource.Text = "Delete Source File After Conversion"
        '
        'btnBrowse
        '
        Me.btnBrowse.Location = New System.Drawing.Point(344, 8)
        Me.btnBrowse.Name = "btnBrowse"
        Me.btnBrowse.TabIndex = 3
        Me.btnBrowse.Text = "Browse"
        '
        'lblStatus
        '
        Me.lblStatus.Location = New System.Drawing.Point(0, 80)
        Me.lblStatus.Name = "lblStatus"
        Me.lblStatus.Size = New System.Drawing.Size(424, 23)
        Me.lblStatus.TabIndex = 4
        '
        '_TestingForm
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(424, 101)
        Me.Controls.Add(Me.lblStatus)
        Me.Controls.Add(Me.btnBrowse)
        Me.Controls.Add(Me.chkDeleteSource)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.txtFolderLocation)
        Me.Name = "_TestingForm"
        Me.Text = "_TestingForm"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
        Dim MyDialog As New FolderBrowserDialog
        ' Sets the default location
        MyDialog.SelectedPath = txtFolderLocation.Text.ToString.Trim

        ' Update the text box folder if the user clicks OK
        If (MyDialog.ShowDialog() = DialogResult.OK) Then
            txtFolderLocation.Text = MyDialog.SelectedPath
        End If
    End Sub

    Private Sub txtFolderLocation_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtFolderLocation.Click
        Me.txtFolderLocation.Text = ""
    End Sub

    Private Sub _TestingForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If txtFolderLocation.Text.ToString.Trim = "" Then
            txtFolderLocation.Text = System.IO.Directory.GetParent(Application.ExecutablePath).ToString
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Me.txtFolderLocation.Text.ToString.Trim <> "" Then
            Call ConvertAllTIFFFiles2JPG(Me.txtFolderLocation.Text.ToString.Trim & "\")

            Me.lblStatus.Text = "Conversion completed..."
        Else
            Call Microsoft.VisualBasic.MsgBox("Please select the folder to convert")
        End If
    End Sub

    Private Sub ConvertAllTIFFFiles2JPG(ByVal DirectoryName As String)
        Dim _DirectoryInfo As New System.IO.DirectoryInfo(DirectoryName)
        'To get files ending with .tiff and call the convert function
        For Each _Files As System.IO.FileInfo In _DirectoryInfo.GetFiles("*.tif?")
            Call ConvertTIFF2JPG(_Files.FullName)
        Next

        'To recursively search inside folders
        For Each _Directory As System.IO.DirectoryInfo In _DirectoryInfo.GetDirectories()
            Call ConvertAllTIFFFiles2JPG(_Directory.FullName)
        Next
    End Sub

    Public Sub ConvertTIFF2JPG(ByVal _TIFFFilePath As String)
        Dim _JPGFile As String
        Try
            Dim _objJPG As New Bitmap(_TIFFFilePath)
            _JPGFile = GetDirectoryName(_TIFFFilePath) + "\" + GetFileNameWithoutExtension(_TIFFFilePath) + "." & ImageFormat.Jpeg.ToString

            'To set the process status
            Me.lblStatus.Text = "Converting " + _TIFFFilePath + " to " + _JPGFile + "..."

            'To delete if target file already exist
            If System.IO.File.Exists(_JPGFile) Then Call System.IO.File.Delete(_JPGFile)

            'To convert source tiff file as .jpg file
            _objJPG.Save(_JPGFile, ImageFormat.Jpeg)

            'To delete Source file after converting it sucessfully
            If Me.chkDeleteSource.Checked Then
                Me.lblStatus.Text = "Deleting source file " + _TIFFFilePath + "..."
                Call System.IO.File.Delete(_TIFFFilePath)
            End If
        Catch
            'Do nothing
        End Try
    End Sub
End Class


Bye
Ajai
0
 
LVL 11

Expert Comment

by:ajaikumarr
ID: 11741912
Hai,

The above code will search the tiff files on selected folder and then convert it to file.jpg recursively (Along with sub folders)... and also you have an option to delete the source tiff file after converting it jpg.

Hope it helps you.

Bye
Ajai
0
 

Author Comment

by:PLavelle
ID: 11752036
AerosSaga,

I usually do this with Photoshop which produces a 1MB JPEG from a 2MB TIFF. When I tried it with your code, the resulting JPEG was 100K. Is there any way to make the resulting JPEG higher quality?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 11

Expert Comment

by:ajaikumarr
ID: 11752120
Hai,

Yep you can... Check the following link.
http://www.dotnet247.com/247reference/msgs/52/261965.aspx

Bye
Ajai
0
 

Author Comment

by:PLavelle
ID: 11752166
Ajai, can you post some code? There doesn't seem to be anything on that link.
0
 
LVL 17

Assisted Solution

by:AerosSaga
AerosSaga earned 500 total points
ID: 11752192
Just incorporate this code it will allow you to alter the quality

Private Function GetEncoderInfo(ByVal mimeType As String) As ImageCodecInfo
Dim j As Integer
Dim encoders As ImageCodecInfo()
encoders = ImageCodecInfo.GetImageEncoders()
For j = 0 To encoders.Length
If encoders(j).MimeType = mimeType Then
Return encoders(j)
End If
Next j
Return Nothing
End Function

Private Sub SaveJPGWithCompressionSetting(ByVal image As Image, ByVal
szFileName As String, ByVal lCompression As Long)
Dim eps As EncoderParameters = New EncoderParameters(1)
eps.Param(0) = New EncoderParameter(Encoder.Quality, lCompression)
Dim ici As ImageCodecInfo = GetEncoderInfo("image/jpeg")
image.Save(szFileName, ici, eps)
End Sub

Regards,

Aeros
0
 

Author Comment

by:PLavelle
ID: 11787730
Thanks for the help everyone.
0
 

Expert Comment

by:brickwork
ID: 32146979
Stream _stream = new FileStream("C:\\abc.tiff"path, System.IO.FileMode.Open);
MemoryStream storeStream = new MemoryStream();
storeStream.SetLength(_stream.Length);
_stream.Read(storeStream.GetBuffer(), 0, (int)_stream.Length);
storeStream.Flush();
_stream.Close();
Response.ContentType = "image/bmp";
storeStream.WriteTo(Response.OutputStream);
SaveMemoryStream(storeStream, "C:\\text1" + ".bmp");
storeStream.Close();
Response.End();
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now