Advertisement
Guest User

Noch ein allgemeiner Fehler in GDI+ !!!

a guest
Jul 4th, 2014
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 18.91 KB | None | 0 0
  1. Imports System.IO
  2. Imports System.Drawing.Imaging
  3. Public Class GalleryGenerator
  4.     Dim stylesheetfile As String
  5.     Dim stylesheetname As String
  6.     Public Function RotateImg(ByVal sImageFilePath As String) As Boolean
  7.         Dim rft As RotateFlipType = RotateFlipType.RotateNoneFlipNone
  8.         Dim img As Bitmap = Image.FromFile(sImageFilePath)
  9.         Dim properties As PropertyItem() = img.PropertyItems
  10.         Dim bReturn As Boolean = False
  11.         For Each p As PropertyItem In properties
  12.             If p.Id = 274 Then
  13.                 Dim orientation As Short = BitConverter.ToInt16(p.Value, 0)
  14.                 Select Case orientation
  15.                     Case 1
  16.                         rft = RotateFlipType.RotateNoneFlipNone
  17.                     Case 3
  18.                         rft = RotateFlipType.Rotate180FlipNone
  19.                     Case 6
  20.                         rft = RotateFlipType.Rotate90FlipNone
  21.                     Case 8
  22.                         rft = RotateFlipType.Rotate270FlipNone
  23.                 End Select
  24.             End If
  25.         Next
  26.         If rft <> RotateFlipType.RotateNoneFlipNone Then
  27.             img.RotateFlip(rft)
  28.             System.IO.File.Delete(sImageFilePath)
  29.             img.Save(sImageFilePath, System.Drawing.Imaging.ImageFormat.Jpeg)
  30.             bReturn = True
  31.         End If
  32.         Return bReturn
  33.  
  34.     End Function
  35.     Public Class AlphanumComparator
  36.         Implements IComparer
  37.  
  38.         Public Function Compare(ByVal x As Object,
  39.            ByVal y As Object) As Integer Implements IComparer.Compare
  40.  
  41.             ' [1] Validate the arguments.
  42.             Dim s1 As String = x
  43.             If s1 = Nothing Then
  44.                 Return 0
  45.             End If
  46.  
  47.             Dim s2 As String = y
  48.             If s2 = Nothing Then
  49.                 Return 0
  50.             End If
  51.  
  52.             Dim len1 As Integer = s1.Length
  53.             Dim len2 As Integer = s2.Length
  54.             Dim marker1 As Integer = 0
  55.             Dim marker2 As Integer = 0
  56.  
  57.             ' [2] Loop over both Strings.
  58.             While marker1 < len1 And marker2 < len2
  59.  
  60.                 ' [3] Get Chars.
  61.                 Dim ch1 As Char = s1(marker1)
  62.                 Dim ch2 As Char = s2(marker2)
  63.  
  64.                 Dim space1(len1) As Char
  65.                 Dim loc1 As Integer = 0
  66.                 Dim space2(len2) As Char
  67.                 Dim loc2 As Integer = 0
  68.  
  69.                 ' [4] Collect digits for String one.
  70.                 Do
  71.                     space1(loc1) = ch1
  72.                     loc1 += 1
  73.                     marker1 += 1
  74.  
  75.                     If marker1 < len1 Then
  76.                         ch1 = s1(marker1)
  77.                     Else
  78.                         Exit Do
  79.                     End If
  80.                 Loop While Char.IsDigit(ch1) = Char.IsDigit(space1(0))
  81.  
  82.                 ' [5] Collect digits for String two.
  83.                 Do
  84.                     space2(loc2) = ch2
  85.                     loc2 += 1
  86.                     marker2 += 1
  87.  
  88.                     If marker2 < len2 Then
  89.                         ch2 = s2(marker2)
  90.                     Else
  91.                         Exit Do
  92.                     End If
  93.                 Loop While Char.IsDigit(ch2) = Char.IsDigit(space2(0))
  94.  
  95.                 ' [6] Convert to Strings.
  96.                 Dim str1 = New String(space1)
  97.                 Dim str2 = New String(space2)
  98.  
  99.                 ' [7] Parse Strings into Integers.
  100.                 Dim result As Integer
  101.                 If Char.IsDigit(space1(0)) And Char.IsDigit(space2(0)) Then
  102.                     Dim thisNumericChunk = Integer.Parse(str1)
  103.                     Dim thatNumericChunk = Integer.Parse(str2)
  104.                     result = thisNumericChunk.CompareTo(thatNumericChunk)
  105.                 Else
  106.                     result = str1.CompareTo(str2)
  107.                 End If
  108.  
  109.                 ' [8] Return result if not equal.
  110.                 If Not result = 0 Then
  111.                     Return result
  112.                 End If
  113.             End While
  114.  
  115.             ' [9] Compare lengths.
  116.             Return len1 - len2
  117.         End Function
  118.     End Class
  119.     Private Sub GenGalleryButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GenGalleryButton.Click
  120.         Dim indir As New IO.DirectoryInfo(InDirFolderBrowserDialog.SelectedPath)
  121.         Dim outdir As New IO.DirectoryInfo(OutDirFolderBrowserDialog.SelectedPath)
  122.         Dim newdirs As Array = {"scaled", "thumbs", "fullsize", "html"}
  123.         For Each newdir In newdirs
  124.             If Not Directory.Exists(newdir) Then
  125.                 MkDir(newdir)
  126.             End If
  127.         Next
  128.         Dim files As IO.FileInfo() = indir.GetFiles()
  129.         Dim file As IO.FileInfo
  130.         Dim imgnamenoext As New List(Of String)
  131.         Dim imgname As New List(Of String)
  132.         'list the names of all files in the specified directory
  133.         Dim scaledsize As New Single
  134.         Dim thumbsize As New Single
  135.  
  136.         If ImgMaxSizeTextBox.Text > "850" Then
  137.             MsgBox("Size exceeds limit. Using maximum.")
  138.             scaledsize = "850"
  139.         ElseIf ImgMaxSizeTextBox.Text < "300" Then
  140.             MsgBox("Size is below the limit. Using minimum.")
  141.             scaledsize = "300"
  142.         Else
  143.             scaledsize = ImgMaxSizeTextBox.Text
  144.         End If
  145.  
  146.         If ThumbMaxSizeTextBox.Text > "250" Then
  147.             MsgBox("Size exceeds limit. Using maximum.")
  148.             thumbsize = "250"
  149.         ElseIf ThumbMaxSizeTextBox.Text < "50" Then
  150.             MsgBox("Size is below the limit. Using minimum.")
  151.             thumbsize = "50"
  152.         Else
  153.             thumbsize = ThumbMaxSizeTextBox.Text
  154.         End If
  155.  
  156.         For Each file In files
  157.             If (file IsNot Nothing) And (file.Extension.ToUpper = (FormatInDDB.Text.ToUpper)) Then
  158.  
  159.                 imgname.Add(file.FullName)
  160.  
  161.                 imgnamenoext.Add(Path.GetFileNameWithoutExtension(file.FullName))
  162.  
  163.                 RotateImg(file.FullName)
  164.  
  165.                 Dim bm_source As New Bitmap(Image.FromFile(file.FullName))
  166.  
  167.                 Select Case FormatOutDDB.Text
  168.                     Case ".JPG"
  169.                         bm_source.Save(outdir.FullName & "\fullsize\" & Path.GetFileNameWithoutExtension(file.FullName) & ".JPG", System.Drawing.Imaging.ImageFormat.Jpeg) ''STACK-TRACE FUEHRT HIER HIN
  170.                     Case ".PNG"
  171.                         bm_source.Save(outdir.FullName & "\fullsize\" & Path.GetFileNameWithoutExtension(file.FullName) & ".PNG", System.Drawing.Imaging.ImageFormat.Png)
  172.                     Case ".GIF"
  173.                         bm_source.Save(outdir.FullName & "\fullsize\" & Path.GetFileNameWithoutExtension(file.FullName) & ".GIF", System.Drawing.Imaging.ImageFormat.Gif)
  174.                     Case ".TIFF"
  175.                         bm_source.Save(outdir.FullName & "\fullsize\" & Path.GetFileNameWithoutExtension(file.FullName) & ".TIFF", System.Drawing.Imaging.ImageFormat.Tiff)
  176.                     Case ".BMP"
  177.                         bm_source.Save(outdir.FullName & "\fullsize\" & Path.GetFileNameWithoutExtension(file.FullName) & ".BMP", System.Drawing.Imaging.ImageFormat.Bmp)
  178.                 End Select
  179.  
  180.                 Dim scale_factor_sc As New Single
  181.  
  182.                 If bm_source.Height > bm_source.Width Then
  183.                     scale_factor_sc = scaledsize / bm_source.Height
  184.                 Else
  185.                     scale_factor_sc = scaledsize / bm_source.Width
  186.                 End If
  187.  
  188.                 Dim bm_dest_sc As New Bitmap( _
  189.                     CInt(bm_source.Width * scale_factor_sc), _
  190.                     CInt(bm_source.Height * scale_factor_sc))
  191.  
  192.                 Dim gr_dest_sc As Graphics = Graphics.FromImage(bm_dest_sc)
  193.                 gr_dest_sc.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
  194.  
  195.                 gr_dest_sc.DrawImage(bm_source, 0, 0, _
  196.                     bm_dest_sc.Width, _
  197.                     bm_dest_sc.Height)
  198.  
  199.                 Select Case FormatOutDDB.Text
  200.                     Case ".JPG"
  201.                         bm_dest_sc.Save(outdir.FullName & "\scaled\sc_" & Path.GetFileNameWithoutExtension(file.FullName) & ".JPG", System.Drawing.Imaging.ImageFormat.Jpeg)
  202.                     Case ".PNG"
  203.                         bm_dest_sc.Save(outdir.FullName & "\scaled\sc_" & Path.GetFileNameWithoutExtension(file.FullName) & ".PNG", System.Drawing.Imaging.ImageFormat.Png)
  204.                     Case ".GIF"
  205.                         bm_dest_sc.Save(outdir.FullName & "\scaled\sc_" & Path.GetFileNameWithoutExtension(file.FullName) & ".GIF", System.Drawing.Imaging.ImageFormat.Gif)
  206.                     Case ".TIFF"
  207.                         bm_dest_sc.Save(outdir.FullName & "\scaled\sc_" & Path.GetFileNameWithoutExtension(file.FullName) & ".TIFF", System.Drawing.Imaging.ImageFormat.Tiff)
  208.                     Case ".BMP"
  209.                         bm_dest_sc.Save(outdir.FullName & "\scaled\sc_" & Path.GetFileNameWithoutExtension(file.FullName) & ".BMP", System.Drawing.Imaging.ImageFormat.Bmp)
  210.                 End Select
  211.  
  212.                 Dim scale_factor_th As New Single
  213.  
  214.                 If bm_source.Height > bm_source.Width Then
  215.                     scale_factor_th = thumbsize / bm_source.Height
  216.                 Else
  217.                     scale_factor_th = thumbsize / bm_source.Width
  218.                 End If
  219.  
  220.                 Dim bm_dest_th As New Bitmap( _
  221.                     CInt(bm_source.Width * scale_factor_th), _
  222.                     CInt(bm_source.Height * scale_factor_th))
  223.  
  224.                 Dim gr_dest_th As Graphics = Graphics.FromImage(bm_dest_th)
  225.                 gr_dest_th.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
  226.  
  227.                 gr_dest_th.DrawImage(bm_source, 0, 0, _
  228.                     bm_dest_th.Width, _
  229.                     bm_dest_th.Height)
  230.                 Select Case FormatOutDDB.Text
  231.                     Case ".JPG"
  232.                         bm_dest_th.Save(outdir.FullName & "\thumbs\th_" & Path.GetFileNameWithoutExtension(file.FullName) & ".JPG", System.Drawing.Imaging.ImageFormat.Jpeg)
  233.                     Case ".PNG"
  234.                         bm_dest_th.Save(outdir.FullName & "\thumbs\th_" & Path.GetFileNameWithoutExtension(file.FullName) & ".PNG", System.Drawing.Imaging.ImageFormat.Png)
  235.                     Case ".GIF"
  236.                         bm_dest_th.Save(outdir.FullName & "\thumbs\th_" & Path.GetFileNameWithoutExtension(file.FullName) & ".GIF", System.Drawing.Imaging.ImageFormat.Gif)
  237.                     Case ".TIFF"
  238.                         bm_dest_th.Save(outdir.FullName & "\thumbs\th_" & Path.GetFileNameWithoutExtension(file.FullName) & ".TIFF", System.Drawing.Imaging.ImageFormat.Tiff)
  239.                     Case ".BMP"
  240.                         bm_dest_th.Save(outdir.FullName & "\thumbs\th_" & Path.GetFileNameWithoutExtension(file.FullName) & ".BMP", System.Drawing.Imaging.ImageFormat.Bmp)
  241.                 End Select
  242.             End If
  243.         Next
  244.  
  245.         Dim imgnamenoextsort As Array
  246.         imgnamenoextsort = imgnamenoext.ToArray
  247.         Array.Sort(imgnamenoextsort, New AlphanumComparator())
  248.         My.Computer.FileSystem.WriteAllText("index.html", "<!DOCTYPE html " & vbCrLf & "  PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN""" & vbCrLf & "  ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">" & vbCrLf & "<html xmlns=""http://www.w3.org/1999/xhtml"">" & vbCrLf & "<head>" & vbCrLf & "  <title>" & PageTitleTextBox.Text & vbCrLf & "  </title>" & vbCrLf & "  <meta http-equiv=""Content-Type"" content=""text/html; charset=UTF-8"" />" & vbCrLf & "  <link rel=""stylesheet"" type=""text/css"" href=""./" & stylesheetname & """ />" & vbCrLf & "  <style type=""text/css"">" & vbCrLf & "    a {width: ""$maxpx_th""px; display:inline-block; text-align:center; margin:0px; padding:0px;}" & vbCrLf & "  </style>" & vbCrLf & "</head>" & vbCrLf & "<body>", True)
  249.         Dim imgpg As String
  250.         For Each img In imgnamenoextsort
  251.             imgpg = img & ".html"
  252.             My.Computer.FileSystem.WriteAllText("index.html", "  <a href=""html/ht_" & imgpg & """>" & vbCrLf & "    <img src=""thumbs/th_" & img & FormatOutDDB.Text & """ alt=""" & img & FormatOutDDB.Text & """ />" & vbCrLf & "  </a>", True)
  253.         Next
  254.         My.Computer.FileSystem.WriteAllText("index.html", "</body>" & vbCrLf & "</html>", True)
  255.  
  256.         If MagScriptCheckBox.Checked = True Then
  257.             Dim sc As Object = CreateObject("Shell.Application")
  258.             My.Computer.Network.DownloadFile("http://www.nihilogic.dk/labs/mojomagnify/mojomagnify.zip", CurDir() & "\mojomagnify.zip")
  259.             Dim input As Shell32.Folder = sc.NameSpace(CurDir() & "\mojomagnify.zip")
  260.             Dim output As Shell32.Folder = sc.NameSpace(CurDir)
  261.             output.CopyHere(input.Items, 4)
  262.         End If
  263.  
  264.         ChDir("html")
  265.         Dim prevpage As String
  266.         Dim nextpage As String
  267.         For Each img In imgnamenoextsort
  268.             imgpg = "ht_" & img & ".html"
  269.             My.Computer.FileSystem.WriteAllText(imgpg, "<!DOCTYPE html " & vbCrLf & "  ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">" & vbCrLf & "<html xmlns=""http://www.w3.org/1999/xhtml"" xml:lang=""de_DE"" lang=""de_DE"">" & vbCrLf & "<head>" & vbCrLf & "  <title>" & PageTitleTextBox.Text & vbCrLf & "  </title>" & vbCrLf & "  <meta http-equiv=""Content-Type"" content=""text/html; charset=UTF-8"" />" & vbCrLf & "  <link type=""text/css"" href=""../" & stylesheetname & """ rel=""stylesheet"" />" & vbCrLf, True)
  270.             If MagScriptCheckBox.Checked = True Then
  271.                 My.Computer.FileSystem.WriteAllText(imgpg, "<link type=""text/css"" href=""../mojomagnify.css"" rel=""stylesheet"" />" & vbCrLf & "  <script type=""text/javascript"" src=""../mojomagnify.js""></script>", True)
  272.             End If
  273.             My.Computer.FileSystem.WriteAllText(imgpg, vbCrLf & "</head>" & vbCrLf & "<body>" & vbCrLf & "  <div>" & vbCrLf & "    <p onclick=""return false;"">" & vbCrLf & "      <img src=""../scaled/sc_" & img & FormatOutDDB.Text & """ alt=""" & img & FormatOutDDB.Text & """", True)
  274.             If MagScriptCheckBox.Checked = True Then
  275.                 My.Computer.FileSystem.WriteAllText(imgpg, " data-magnifysrc=""../fullsize/" & img & FormatOutDDB.Text & """", True)
  276.             End If
  277.  
  278.             If Array.IndexOf(imgnamenoextsort, img) = "0" Then
  279.                 prevpage = "ht_" & imgnamenoextsort(UBound(imgnamenoextsort)) & ".html"
  280.             Else
  281.                 prevpage = "ht_" & imgnamenoextsort(Array.IndexOf(imgnamenoextsort, img) - 1) & ".html"
  282.             End If
  283.  
  284.             If Array.IndexOf(imgnamenoextsort, img) = UBound(imgnamenoextsort) Then
  285.                 nextpage = "ht_" & imgnamenoextsort(0) & ".html"
  286.             Else
  287.                 nextpage = "ht_" & imgnamenoextsort(Array.IndexOf(imgnamenoextsort, img) + 1) & ".html"
  288.             End If
  289.  
  290.             My.Computer.FileSystem.WriteAllText(imgpg, " />" & vbCrLf & "    </p>" & vbCrLf & "  </div>     " & vbCrLf & "  <div>" & vbCrLf & "    <table>" & vbCrLf & "      <tr>" & vbCrLf & "        <td align=""left"">" & vbCrLf & "          <a href=""" & prevpage & """>" & vbCrLf & "            &#9664; previous" & vbCrLf & "          </a>" & vbCrLf & "        </td>" & vbCrLf & "        <td>" & vbCrLf & "          <p>Entry " & Array.IndexOf(imgnamenoextsort, img) & " of " & UBound(imgnamenoextsort) & "</p>" & vbCrLf & "        </td>" & vbCrLf & "        <td align=""right"">" & vbCrLf & "          <a href=""" & nextpage & """>" & vbCrLf & "            &#9654; next" & vbCrLf & "          </a>" & vbCrLf & "        </td>" & vbCrLf & "        <td align=""right"">" & vbCrLf & "          <a href=""../index.html"">" & vbCrLf & "            &#9650; index" & vbCrLf & "          </a>" & vbCrLf & "        </td>" & vbCrLf & "      </tr>" & vbCrLf & "    </table>", True)
  291.  
  292.             If MagScriptCheckBox.Checked = True Then
  293.                 My.Computer.FileSystem.WriteAllText(imgpg, vbCrLf & "    <noscript><h4>Allow JavaScript for zooming!</h3></noscript>", True)
  294.             End If
  295.  
  296.             My.Computer.FileSystem.WriteAllText(imgpg, vbCrLf & "  </div>" & vbCrLf & "</body>" & vbCrLf & "</html>", True)
  297.         Next
  298.  
  299.         FileCopy(stylesheetfile, "C:\" & stylesheetname) ''PROBLEMVERURSACHER... ABER WIE???
  300.     End Sub
  301.  
  302.     Private Sub InitInFolderDialogueButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InitInFolderDialogueButton.Click
  303.         If InDirFolderBrowserDialog.ShowDialog() = DialogResult.OK Then
  304.             Dim indir As New IO.DirectoryInfo(InDirFolderBrowserDialog.SelectedPath)
  305.             InDirToolTip.SetToolTip(InitInFolderDialogueButton, "Input Directory: " & indir.FullName)
  306.         End If
  307.     End Sub
  308.  
  309.     Private Sub InitOutFolderDialogueButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InitOutFolderDialogueButton.Click
  310.         If OutDirFolderBrowserDialog.ShowDialog() = DialogResult.OK Then
  311.             Dim outdir As New IO.DirectoryInfo(OutDirFolderBrowserDialog.SelectedPath)
  312.             OutDirToolTip.SetToolTip(InitOutFolderDialogueButton, "Output Directory: " & outdir.FullName)
  313.             FileSystem.ChDir(outdir.FullName)
  314.         End If
  315.     End Sub
  316.  
  317.     Private Sub GalleryGenerator_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  318.         Dim indir As New IO.DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments))
  319.         InDirFolderBrowserDialog.SelectedPath = indir.FullName
  320.         FormatInDDB.SelectedIndex = 0
  321.         InDirToolTip.SetToolTip(InitInFolderDialogueButton, "Input Directory: " & indir.FullName & " (default)")
  322.         Dim outdir As New IO.DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments))
  323.         OutDirFolderBrowserDialog.SelectedPath = outdir.FullName
  324.         FormatOutDDB.SelectedIndex = 0
  325.         FileSystem.ChDir(outdir.FullName)
  326.         OutDirToolTip.SetToolTip(InitOutFolderDialogueButton, "Output Directory: " & outdir.FullName & " (default)")
  327.         ImgMaxSizeTextBox.MaxLength = 3
  328.         ThumbMaxSizeTextBox.MaxLength = 3
  329.     End Sub
  330.  
  331.     Private Sub ImgMaxSizeTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ImgMaxSizeTextBox.KeyPress
  332.         If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) Then
  333.             e.Handled = True
  334.         End If
  335.     End Sub
  336.  
  337.     Private Sub ThumbMaxSizeTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ThumbMaxSizeTextBox.KeyPress
  338.         If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) Then
  339.             e.Handled = True
  340.         End If
  341.     End Sub
  342.  
  343.     Private Sub CSSBrowseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CSSBrowseButton.Click
  344.         Using CSSBrowseDialog As New OpenFileDialog ''GELOEST DURCH USING-STATEMENT
  345.             CSSBrowseDialog.ShowDialog()
  346.             stylesheetfile = CSSBrowseDialog.FileName
  347.             stylesheetname = CSSBrowseDialog.SafeFileName
  348.         End Using
  349.     End Sub
  350. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement