I share for you all these VB.NET snippets

Jun 18, 2014 at 9:52 PM
Hi

Just I've discovered this awesome wrapper and I would like to share some snippets that I've wrote, are basically the same as the examples of the officlal documentation, but a little bit modified and for all those people who does not have sufficient knowledges to translate them to VB.NET:

I hope this could help someone :).
        ' =============================
        ' Read basic image information:
        ' =============================

        ' Read image from file.
        Dim img As New MagickImage("C:\Image.jpg")

        ' Read info from 'MagickImage' object.
        Dim info As New MagickImageInfo(img)

        Dim sb As New System.Text.StringBuilder
        With sb
            .AppendLine(String.Format("Width: {0}", info.Width))
            .AppendLine(String.Format("Height: {0}", info.Height))
            .AppendLine(String.Format("ColorSpace: {0}", info.ColorSpace))
            .AppendLine(String.Format("Format: {0}", info.Format))
            .AppendLine(String.Format("ResolutionX: {0}", info.ResolutionX))
            .AppendLine(String.Format("ResolutionY: {0}", info.ResolutionY))
        End With

        MessageBox.Show(sb.ToString, "Image Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
        ' =============================================
        ' Read image with multiple layers/frames (gif):
        ' =============================================

        ' Read image collection from file.
        Dim imgCollection As New MagickImageCollection("C:\1.gif")

        ' Iterate frames.
        For Frame As Integer = 0 To (imgCollection.Count - 1)

            Dim sb As New System.Text.StringBuilder
            With sb
                .AppendLine(String.Format("Animation Delay: {0}", imgCollection(Frame).AnimationDelay))
                .AppendLine(String.Format("Total Colors: {0}", imgCollection(Frame).TotalColors))
            End With

            MessageBox.Show(sb.ToString, String.Format("Image Frame: {0}", CStr(Frame)),
                            MessageBoxButtons.OK, MessageBoxIcon.Information)

        Next Frame
        ' ==================================
        ' Read Exif information of an image:
        ' ==================================

        ' Read image from file.
        Using img As New MagickImage("C:\Image.jpg")

            ' Retrieve the Exif Information (if any)
            Dim ExifData As ExifProfile = img.GetExifProfile()

            Select Case ExifData Is Nothing

                Case True ' Any Exif values found.
                    MessageBox.Show("The image does not contains Exif information", "Image Information",
                                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

                Case Else ' At least 1 Exif value found.

                    ' StringBuilder to format the Exif entries.
                    Dim sb As New System.Text.StringBuilder

                    ' Iterate the Exif values.
                    For Each Value As ExifValue In ExifData.Values

                        With sb
                            .AppendLine(String.Format("{0}({1}): {2}",
                                                      Value.Tag,
                                                      Value.DataType,
                                                      Value.ToString()))
                        End With '/ sb

                    Next Value

                    MessageBox.Show(sb.ToString, "Exif Image Information",
                        MessageBoxButtons.OK, MessageBoxIcon.Information)

            End Select '/ ExifData Is Nothing

        End Using '/ img As New MagickImage
        ' =============
        ' Merge images:
        ' =============
        Using imgCollection As New MagickImageCollection()

            ' Note:
            ' The first image is the background layer and the second image is the top layer;
            ' so the width/height of the 2nd image should be smaller than the 1st image to see both.

            Dim FirstImage As New MagickImage("C:\1.jpg")
            Dim SecondImage As New MagickImage("C:\2.jpg")

            With imgCollection
                .Add(FirstImage)
                .Add(SecondImage)
            End With '/ imgCollection

            ' Save the merged image to disk.
            Using MergedImage As New MagickImage(imgCollection.Mosaic)
                MergedImage.Write("C:\Mosaic.jpg")
            End Using '/ MergedImage As New MagickImage

        End Using '/ imgCollection As New MagickImageCollection
        ' =======================
        ' Create an animated Gif:
        ' =======================

        Using Gif As New MagickImageCollection()

            ' Add the first frame.
            Gif.Add("C:\1.jpg")
            Gif.Last.AnimationDelay = 50 ' Specify an Animation delay for this frame.

            ' Add the second frame.
            Gif.Add("C:\2.jpg")
            Gif.Last.AnimationDelay = 50 ' Specify an Animation delay for this frame.
            Gif.Last.Flip() ' Optionally specify a (flip) effect too.

            ' Optionally reduce frame colors.
            Dim GifSettings As New QuantizeSettings()
            GifSettings.Colors = 256
            Gif.Quantize(GifSettings)

            ' Optionally optimize the Gif images (Images should be with equal size).
            Gif.Optimize()

            ' Write the Gif to disk.
            Gif.Write("C:\Snakeware.Animated.gif")

        End Using '/ Gif As New MagickImageCollection
Coordinator
Jun 19, 2014 at 8:01 AM
Thanks for sharing this. It is probably a good idea to also have samples in VB. And there is a trick to automatically do this in the sample package described here: http://nuget.codeplex.com/workitem/381#PostedByLink8. I will try to see if I can get this to work.