Public Class frmPicList
Dim ofd As New OpenFileDialog With _
{.Title = "Ein Bild laden", .Filter = _
"Bilder|*.bmp;*.png;*.tif;*.jpg;*.gif"}
Dim WithEvents btnLoad As New Button _
With {.Parent = Me, .Text = "Ein Bild laden", .Width = 100}
Dim fbd As New FolderBrowserDialog _
With {.Description = "Bilder speichern (PNG)", .ShowNewFolderButton = True}
Dim WithEvents btnSave As New Button With _
{.Parent = Me, .Text = "Alle Bilder speichern (PNG)", .Width = 200, .Left = _
100}
Dim dgv As New DataGridView With {.Parent = Me}
Class picEntry
Dim _key As String
Dim _Daten() As Byte
Public Sub New(ByVal filename As String)
_Daten = My.Computer.FileSystem.ReadAllBytes(filename)
_key = IO.Path.GetFileNameWithoutExtension(filename)
End Sub
Public Function Bild() As Bitmap
Dim bmp As Bitmap
Using picstream As New IO.MemoryStream(_Daten)
bmp = CType(Bitmap.FromStream(picstream), Bitmap)
End Using
Return bmp
End Function
Public ReadOnly Property Thumbnail() As Bitmap
Get
Dim bmp As Bitmap = Bild
Dim srect As New Rectangle(0, 0, bmp.Width, bmp.Height)
Dim drect As New Rectangle _
(0, 0, CInt(100 / bmp.Height * bmp.Width), 100)
Dim tbmp As New Bitmap(drect.Width, drect.Height)
Using g As Graphics = Graphics.FromImage(tbmp)
g.DrawImage(bmp, drect, srect, GraphicsUnit.Pixel)
End Using
Return tbmp
End Get
End Property
Public ReadOnly Property key As String
Get
Return _key
End Get
End Property
End Class
Dim picList As New System.ComponentModel.BindingList(Of picEntry)
Private Sub frmPicList_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
dgv.DataSource = picList
frmPicList_Resize(Me, New EventArgs)
End Sub
Private Sub frmPicList_Resize(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Resize
With dgv
.Top = 25 : .Left = 4
.Width = Me.Width - 10 : .Height = Me.Height - 60
End With
End Sub
Private Sub btnLoad_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles btnLoad.Click
If ofd.ShowDialog = DialogResult.OK Then
picList.Add(New picEntry(ofd.FileName))
dgv.Rows(dgv.Rows.Count - 1).Height = 100
End If
End Sub
Private Sub btnSave_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles btnSave.Click
If fbd.ShowDialog = DialogResult.OK Then
For i As Integer = 0 To picList.Count - 1
With picList(i)
Dim filepath As String = _
IO.Path.Combine(fbd.SelectedPath, .key & ".png")
Dim bmp As Bitmap = picList(i).Bild
Using bmp_out As New Bitmap(bmp.Width, bmp.Height, _
Imaging.PixelFormat.Format24bppRgb),
g As Graphics = Graphics.FromImage(bmp_out)
g.DrawImage(bmp, 0, 0, bmp.Width, bmp.Height)
bmp_out.Save(filepath, Imaging.ImageFormat.Png)
End Using
End With
Next i
End If
End Sub
End Class |