Hi Leute,
da ich hier beim Suchen nicht wirklich das gefunden habe, was ich suchte, möchte ich Euch meinen Code posten, mit dem eine animated Gif erstellt werden kann.
Folgende NameSpaces müssen importiert werden:
Folgende DLLs müssen dem Projekt hinzugefügt werden:
- PresentationCore;
- WindowsBase;
- System.Xaml
|
Visual Basic Quellcode
|
1
2
3
|
Imports System.Windows.Media
Imports System.Windows.Media.Imaging ' GIF
Imports System.Runtime.InteropServices ' Marshal
|
Hier klicken für weitere Informationen
|
Visual Basic Quellcode
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
Public Class Form1
' Dictionary zur Zuordnung der Formatdefinitionen in den 2 Namespaces
Private Formats As Dictionary(Of System.Drawing.Imaging.PixelFormat, System.Windows.Media.PixelFormat)
Public Sub New()
InitializeComponent()
' Dictionary anlegen
Formats = New Dictionary(Of System.Drawing.Imaging.PixelFormat, System.Windows.Media.PixelFormat)
' Dictionary und befüllen
Formats.Add(System.Drawing.Imaging.PixelFormat.Format32bppArgb, PixelFormats.Pbgra32)
Formats.Add(System.Drawing.Imaging.PixelFormat.Format24bppRgb, PixelFormats.Bgr24)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Quell-Liste bereitstellen
Dim lBmp As New List(Of String)
lBmp.Add("C:\Temp\Bitmap1.bmp")
lBmp.Add("C:\Temp\Bitmap2.bmp")
lBmp.Add("C:\Temp\Bitmap3.bmp")
' Gif-Erstellung aufrufen
DrawMakroGif("c:\Temp\MyGif.gif", lBmp)
End Sub
Friend Sub DrawMakroGif(ByVal fileName As String, ByVal lBmp As List(Of String))
' Test, ob Daten vorhanden sind
If (lBmp.Count = 0) Then
MessageBox.Show("Keine Bilder verfügbar.")
Return
End If
' Encoder zur Erzeugung der Gif
Dim encoder As GifBitmapEncoder = New GifBitmapEncoder()
' Schleife über alle Bilder
For Each file As String In lBmp
' Bitmap erzeugen
Dim bmp As New Bitmap(file)
' Test, ob das Format eingetragen ist
If (Not Formats.ContainsKey(bmp.PixelFormat)) Then
MessageBox.Show("Format nicht verfügbar: " & bmp.PixelFormat.ToString)
Return
End If
' Bitmap-Größe und -Daten holen und festhalten
Dim rc As New Rectangle(0, 0, bmp.Width, bmp.Height)
Dim bmpData As System.Drawing.Imaging.BitmapData
bmpData = bmp.LockBits(rc, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat)
' Feld für die Pixel-Information bereitstellen
Dim pixels() As Byte
ReDim pixels(rc.Height * bmpData.Stride - 1) ' ist halt in VB so
' Kopieren der Pixelinformation
Marshal.Copy(bmpData.Scan0, pixels, 0, pixels.Length)
bmp.UnlockBits(bmpData)
' Bitmap-Source nach dem Bilde der Bitmap erzeugen
Dim _image As BitmapSource = BitmapSource.Create(rc.Width, rc.Height, 96, 96, Formats(bmp.PixelFormat), Nothing, pixels, bmpData.Stride)
' rc.Width - Breite der Bitmap in Pixeln
' rc.Height - Höhe der Bitmap in Pixeln
' 96 - horizontale Punkte pro Zoll (dots per inch, dpi) der Bitmap
' 96 - vertikale Punkte pro Zoll (dots per inch, dpi) der Bitmap.
' Formats(bmp.PixelFormat) - Pixelformat
' Nothing - Define the image palette
' pixels - die Pixel der Bitmap
' bmpData.Stride - Schrittweite der Bitmap
' der Gif hinzufügen
encoder.Frames.Add(BitmapFrame.Create(_image))
Next
' Gif abspeichern
Using _stream As System.IO.FileStream = New System.IO.FileStream(fileName, System.IO.FileMode.Create)
encoder.Save(_stream)
End Using
End Sub
End Class
|
Im Beispiel wird eine einfache Form mit einem Button benötigt. Im Konstruktor wird ein Dictionary zur Zuordnung von PixelFormat in 2 Namespaces bereitgestellt: Das Pixelformat (z.B. 24 Bit Per Pixel) wird im Namespace
System.Drawing.Imaging und im Namespace
System.Windows.Media separat belegt, beide Werte werden benötigt. Deshalb habe ich ein Dictionary zur eindeutigen Zuordnung angelegt. Kommt nur ein Format vor, kann dies natürlich hart codiert werden.
Im Button1_Click()-Event werden die einzelnen Bilder als Liste der Dateipfade angelegt, das kann natürlich auch eine
List(Of Bitmap) oder dergleichen sein.
Die Hauptprozedur des Progreamms
DrawMakroGif wird mit dem Namen der zu erstellenden Gif sowie der Liste der Bitmap-Namen aufgerufen.
Zunächst wird getestet, ob überhaupt Daten vorhanden sind.
Danach wird der Encoder zur Erzeugung der Gif angelegt.
In der Schleife über alle Bilder wird zuerst die Bitmap erzeugt und das Vorhandensein des
Pixelformats getestet. Danach wird ihre Größe ausgelesen und ihre Pixeldaten im Speicher festgehalten.
Das Feld zur Übernahme der Pixel-Daten wird angelegt und mit den Informationen der Bitmap befüllt.
Zuletzt wird noch die
BitmapSource entsprechend dem Bild der Bitmap erzeugt und dem Gif-Encoder hinzugefügt.
Sind alle Bilder hinzugefügt, muss die Gif nur noch abgespeichert werden.
Fertig.
Das besondere an dieser Gif-Erstellung ist, dass der Encoder intern überprüft, wieviele Farben tatsächlich benötigt werden, danach wird die Farbtiefe der Gif festgelegt.
Es ist durchaus sinnvoll, 32-Bit per Pixel Bilder derr Gif hinzuzufügen, obwohl lediglich z.B. 12 Farben vorkommen. Die Gif würde dann mit 4 Bit Per Pixel erzeugt werden.
So kann darauf verzichtet werden, beim Erstellen der BitmapSource eine Palette zu übergeben.