JPG's to Tif multipage convert{Height and width problem)

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
HI!

Ive made an application that convert jpgs to a single tif multipage with public and private code but I have a problem with images height and width
Code is running good without erros but when I convert 10 jpg with different dimensions to a 200 dpi tif multipage some images arent converted so I get on tif only 5 or 6
I dont know what is wrong!
My code:
Code:
 Delegate Sub SetLabelText_Delegate(ByVal [Label] As Label, ByVal [text] As String)<br/>
    Public Shared Sub SetLabelText_ThreadSafe(ByVal [Label] As Label, ByVal [text] As String)<br/>
        If [Label].InvokeRequired Then<br/>
            Dim MyDelegate As New SetLabelText_Delegate(AddressOf SetLabelText_ThreadSafe)<br/>
            Form1.Invoke(MyDelegate, New Object() {[Label], [text]})<br/>
        Else<br/>
            [Label].Text = [text]<br/>
        End If<br/>
    End Sub<br/>
    Public Shared Sub Snooze(ByVal seconds As Integer)<br/>
        For i As Integer = 0 To seconds * 100<br/>
            System.Threading.Thread.Sleep(0.1)<br/>
            Application.DoEvents()<br/>
        Next<br/>
    End Sub<br/>
    Dim x1 As String<br/>
    Dim x2 As String<br/>
    Dim x3 As String<br/>
    Public Sub findmyfiles_NonRecursive(ByRef thelist As List(Of String), ByVal currentdir As String, ByRef lbl As Label, ByRef lbl2 As Label)<br/>
        Dim q As New Queue(Of String)<br/>
        q.Enqueue(currentdir)<br/>
        While q.Count > 0<br/>
            currentdir = q.Peek<br/>
            If lbl2 IsNot Nothing Then<br/>
                SetLabelText_ThreadSafe(lbl2, String.Format(":- {0}", currentdir))<br/>
            End If<br/>
            q.Dequeue()<br/>
            Try<br/>
                For Each files In My.Computer.FileSystem.GetFiles(currentdir, FileIO.SearchOption.SearchTopLevelOnly, "*.jpg")<br/>
                    thelist.Add(files)<br/>
                    Try<br/>
                        x1 = currentdir.Substring(currentdir.LastIndexOf(""))<br/>
                        x2 = x1.Remove(0, 1)<br/>
                        If EvolveCheckBox1.Checked = True Then<br/>
                            EvolveTextBox3.Text = x2.Remove(0, 5)<br/>
                        End If<br/>
                        x3 = EvolveTextBox2.Text & "" & x2 & ".tif"<br/>
                        SaveAddTiff(System.Drawing.Image.FromFile(files), x3)<br/>
                    Catch ex As Exception<br/>
<br/>
                    End Try<br/>
                    If lbl IsNot Nothing Then<br/>
                        SetLabelText_ThreadSafe(lbl, String.Format("{0} Jpg ", thelist.Count))<br/>
                    End If<br/>
                Next<br/>
                For Each dirfound As String In My.Computer.FileSystem.GetDirectories(currentdir, FileIO.SearchOption.SearchTopLevelOnly)<br/>
                    q.Enqueue(dirfound)<br/>
                Next<br/>
            Catch ex As UnauthorizedAccessException<br/>
                q.Dequeue()<br/>
                Continue While<br/>
            End Try<br/>
        End While<br/>
    End Sub<br/>
    Sub SaveAddTiff(ByVal img As Image, ByVal filename As String)<br/>
        If Not IO.File.Exists(filename) Then<br/>
            img.Save(filename, Imaging.ImageFormat.Tiff)<br/>
        Else<br/>
            Dim frames As List(Of Image) = getFrames(filename)<br/>
            frames.Add(img)<br/>
            SaveMultiTiff(frames.ToArray, filename)<br/>
        End If<br/>
        img.Dispose()<br/>
    End Sub<br/>
    Sub SaveMultiTiff(ByVal frames() As Image, ByVal filename As String)<br/>
        Dim cd As Integer = EvolveComboBox1.Text<br/>
        Dim iq As Integer = EvolveComboBox2.Text<br/>
        Dim codec As ImageCodecInfo = getTiffCodec()<br/>
        Dim enc As Encoder = Encoder.SaveFlag<br/>
        Dim ep As New EncoderParameters(4)<br/>
        ep.Param(0) = New EncoderParameter(enc, CLng(EncoderValue.MultiFrame))<br/>
        If EvolveRadiobutton1.Checked = True Then<br/>
            ep.Param(1) = New EncoderParameter(Encoder.Compression, CLng(EncoderValue.CompressionLZW))<br/>
        End If<br/>
        If EvolveRadiobutton2.Checked = True Then<br/>
            ep.Param(1) = New EncoderParameter(Encoder.Compression, CLng(EncoderValue.CompressionCCITT4))<br/>
        End If<br/>
        ep.Param(2) = New EncoderParameter(Encoder.ColorDepth, cd)<br/>
        ep.Param(3) = New EncoderParameter(Encoder.Quality, iq)<br/>
        Dim tiff As Image = frames(0)<br/>
        tiff.Save(filename, codec, ep)<br/>
        ep.Param(0) = New EncoderParameter(enc, CLng(EncoderValue.FrameDimensionPage))<br/>
        For i As Integer = 1 To frames.Length - 1<br/>
            tiff.SaveAdd(frames(i), ep)<br/>
            frames(i).Dispose()<br/>
        Next<br/>
        ep.Param(0) = New EncoderParameter(enc, CLng(EncoderValue.Flush))<br/>
        tiff.SaveAdd(ep)<br/>
        tiff.Dispose()<br/>
    End Sub<br/>
    Function getTiffCodec() As ImageCodecInfo<br/>
        For Each ice As ImageCodecInfo In ImageCodecInfo.GetImageEncoders()<br/>
            If ice.MimeType = "image/tiff" Then<br/>
                Return ice<br/>
            End If<br/>
        Next<br/>
        Return Nothing<br/>
    End Function<br/>
  Function getFrames(ByVal filename) As List(Of Image)<br/>
        Dim h As Integer = EvolveComboBox3.Text<br/>
        Dim v As Integer = EvolveComboBox4.Text<br/>
        Dim frames As New List(Of Image)<br/>
        Dim img As Image = Image.FromFile(filename)<br/>
        For i As Integer = 0 To img.GetFrameCount(Imaging.FrameDimension.Page) - 1<br/>
            Snooze(0.1)<br/>
            img.SelectActiveFrame(Imaging.FrameDimension.Page, i)<br/>
            Dim TmpSize As Size<br/>
            TmpSize.Width = img.Width<br/>
            TmpSize.Height = img.Height<br/>
            Dim tmp As New Bitmap(img, TmpSize)<br/>
            Dim g As Graphics = Graphics.FromImage(tmp)<br/>
            If EvolveCheckBox1.Checked = True Then<br/>
                g.DrawString(EvolveTextBox3.Text, _<br/>
       New Font("Arial Narrow", 50, FontStyle.Regular), _<br/>
       New SolidBrush(Color.FromArgb(250, 120, 110)), 650, 1000)<br/>
            Else<br/>
            End If<br/>
            g.CompositingQuality = Drawing2D.CompositingQuality.HighQuality<br/>
            g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic<br/>
            g.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality<br/>
            g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality<br/>
            g.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAliasGridFit<br/>
            g.DrawImageUnscaled(img, 0, 0)<br/>
            tmp.SetResolution(h, v)<br/>
            frames.Add(tmp)<br/>
            g.Dispose()<br/>
        Next<br/>
        img.Dispose()<br/>
        Return frames<br/>
    End Function



View the full article
 
Back
Top