?
Solved

PNG Compression in VB.Net

Posted on 2005-04-03
7
Medium Priority
?
2,920 Views
Last Modified: 2008-01-09
Hi,

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.
0
Comment
Question by:maykut
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
7 Comments
 
LVL 3

Expert Comment

by:Geo123
ID: 13696389
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......

cheers

George
0
 
LVL 27

Accepted Solution

by:
planocz earned 210 total points
ID: 13697783
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()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        '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
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    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.SuspendLayout()
        '
        'btnOpen
        '
        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"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.Add(Me.btnOpen)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub btnOpen_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOpen.Click
        OpenBMPFile()
        SaveFile()
        MessageBox.Show("Finished")
    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)
        Else
            Me.Close()
        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)
        Else
            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.Clear(Color.White)
        grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic

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

        grPhoto.Dispose()
        Return bmPhoto
    End Function
End Class
0
 
LVL 1

Author Comment

by:maykut
ID: 13703369
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.
0
 
LVL 27

Expert Comment

by:planocz
ID: 13706358
" 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.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question