MakeIco.vb
''
'' This code is part of Document Solutions for Imaging demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System
Imports System.IO
Imports System.Drawing
Imports GrapeCity.Documents.Imaging

'' This sample loads a large PNG image, resizes it to several smaller sizes
'' commonly used in icons, and creates a multi-frame .ICO from those.
'' See also the ReadIco sample which renders the frames from the .ICO created by this sample.
Public Class MakeIco
    Public ReadOnly Property DefaultMime As String
        Get
            Return Util.MimeTypes.ICO
        End Get
    End Property

    Public Function GenerateImageStream(
            ByVal targetMime As String,
            ByVal pixelSize As Size,
            ByVal dpi As Single,
            ByVal opaque As Boolean,
            Optional ByVal sampleParams As String() = Nothing) As Stream

        If targetMime <> Util.MimeTypes.ICO Then
            Throw New Exception("This sample only supports ICO output format.")
        End If

        Dim srcPath = Path.Combine("Resources", "ImagesBis", "mescius-logomark-c.png")
        Dim srcBmp As New GcBitmap()
        Using s As Stream = File.OpenRead(srcPath)
            srcBmp.Load(s)
        End Using

        Using bmp16 As GcBitmap = srcBmp.Resize(16, 16),
              bmp24 As GcBitmap = srcBmp.Resize(24, 24),
              bmp32 As GcBitmap = srcBmp.Resize(32, 32),
              bmp48 As GcBitmap = srcBmp.Resize(48, 48),
              bmp256 As GcBitmap = srcBmp.Resize(256, 256),
              ico As New GcIco()

            ico.Frames.Add(New IcoFrame(bmp256, IcoFrameEncoding.Png))
            ico.Frames.Add(New IcoFrame(bmp48, IcoFrameEncoding.Png))
            ico.Frames.Add(New IcoFrame(bmp32, IcoFrameEncoding.Png))
            ico.Frames.Add(New IcoFrame(bmp24, IcoFrameEncoding.Png))
            ico.Frames.Add(New IcoFrame(bmp16, IcoFrameEncoding.Png))

            Dim ms = New MemoryStream()
            ico.Save(ms)
            ms.Seek(0, SeekOrigin.Begin)
            Return ms
        End Using
    End Function
End Class