• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2978
  • Last Modified:

PNG Compression in VB.Net


I convert all my bitmap images to PNG files and the file size is reduced dramatically, once I've converted all my graphics to PNG files I use another program called PNGOut and it further reduces my PNG file size. Is their a way that I do the same thing that PNGOut does but do it in VB.Net as a code implementation ie convert Bitmap/GIF/Jpeg to PNG and when saving the PNG files a further compression is done before saving the files once the extra compression is done it then saves the PNG files.
  • 2
1 Solution
framework does have pic convertion stuff, I saw demo on Franklins.net it was wee program that converted graphic files.... the code wasnt much at all......


Here is a sample that I use to make image files, Just change it to save in PNG.....
This might help in your search.....

Imports System.IO
Imports System.Drawing.Imaging
Imports System.Drawing.Drawing2D

Public Class frmMakeImageFile
    Inherits System.Windows.Forms.Form
    Private sFileName As String
    Private imgFile As Image
#Region " Windows Form Designer generated code "

    Public Sub New()

        'This call is required by the Windows Form Designer.

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
            End If
        End If
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents btnOpen As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.btnOpen = New System.Windows.Forms.Button
        Me.btnOpen.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.btnOpen.Location = New System.Drawing.Point(140, 232)
        Me.btnOpen.Name = "btnOpen"
        Me.btnOpen.Size = New System.Drawing.Size(132, 28)
        Me.btnOpen.TabIndex = 5
        Me.btnOpen.Text = "&Open a Image File"
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Name = "Form1"
        Me.Text = "Form1"

    End Sub

#End Region

    Private Sub btnOpen_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOpen.Click
    End Sub
    Private Sub OpenBMPFile()
        Dim i As Short
        Dim myOpenFileDialog As New OpenFileDialog
        With myOpenFileDialog
            .CheckFileExists = True
            .DefaultExt = "jpg"
            .Filter = "Jpeg files(*.jpg;*.jpeg;*.jpe;*.jfif)|*.jpg"
            .Filter += "|Icon files(*.ico)|*.ico"
            .Filter += "|Bmp files (*.bmp)|*.bmp"
            .Filter += "|Gif files(*.gif)|*.gif"
            .Filter += "|Tif files(*.tif;*.tiff)|*.tif"
            .Filter += "|Png files(*.Png)|*.Png"
            .InitialDirectory = "c:\"
            .Multiselect = False
        End With
        ' Use the OpenFileDialog and put the path and name of the selected file in a var.
        If myOpenFileDialog.ShowDialog = DialogResult.OK Then
            sFileName = myOpenFileDialog.FileName
            sIconPath = Path.GetDirectoryName(sFileName) & "\"
            i = sFileName.LastIndexOf("\") + 1
            sFileName = sFileName.Substring(i)
        End If
    End Sub
    Private Sub SaveFile()
        imgFile = Bitmap.FromFile(sFileName)
        imgFile = FixedSize(imgFile, 32, 32)   '<--- Finished size (make it the size you want

        Dim imgOut As Image = New Bitmap(imgFile.Width, imgFile.Height)
        Dim g As Graphics = Graphics.FromImage(imgOut)

        g.DrawImage(imgFile, New Rectangle(0, 0, imgFile.Width, imgFile.Height), New Rectangle(0, 0, imgFile.Width, imgFile.Height), GraphicsUnit.Pixel)
        imgOut.Save("C:\test.ico", ImageFormat.Icon)
    End Sub
    Private Function FixedSize(ByVal imgPhoto As Image, ByVal Width As Integer, ByVal Height As Integer) As Image
        Dim sourceWidth As Integer = imgPhoto.Width
        Dim sourceHeight As Integer = imgPhoto.Height
        Dim sourceX As Integer = 0
        Dim sourceY As Integer = 0
        Dim destX As Integer = 0
        Dim destY As Integer = 0

        Dim nPercent As Single = 0
        Dim nPercentW As Single = 0
        Dim nPercentH As Single = 0

        nPercentW = (CType(Width / CType(sourceWidth, Double), Double))
        nPercentH = (CType(Height / CType(sourceHeight, Double), Double))
        If nPercentH < nPercentW Then
            nPercent = nPercentH
            destX = System.Convert.ToInt16((Width - (sourceWidth * nPercent)) / 2)
            nPercent = nPercentW
            destY = System.Convert.ToInt16((Height - (sourceHeight * nPercent)) / 2)
        End If

        Dim destWidth As Integer = CType((sourceWidth * nPercent), Integer)
        Dim destHeight As Integer = CType((sourceHeight * nPercent), Integer)
        Dim bmPhoto As Bitmap
        bmPhoto = New Bitmap(Width, Height, PixelFormat.Format24bppRgb)
        bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution)

        Dim grPhoto As Graphics = Graphics.FromImage(bmPhoto)
        grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic

        grPhoto.DrawImage(imgPhoto, New Rectangle(destX, destY, destWidth, destHeight), New Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel)

        Return bmPhoto
    End Function
End Class
maykutAuthor Commented:
I don't want to change the size, I still want to retain the same image quality, basically what it should do is get rid of extra chunks that should not be their.
" imgFile = FixedSize(imgFile, 32, 32)   '<--- Finished size (make it the size you want" is for you to change the size
OR maintain the size you need.

The  Function FixedSize is to keep the quality of the image the same.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now