Basic Image Manipulation Flip, Rotate and Quality

J.C. SolvoTerra 1 Tallied Votes 2K Views Share

After trying to manipulate PNG files and failing miserably discovering that the standard GDI doesn't provide support for any PNG parameters decided to provide some basic image manipulation functions built into .Net that I came across on my investigations.

1. Adjust Image Quality
2. Flip Image Horizontally
3. Flip Image Vertically
4. Rotate Image (90 Degree Step)

This Source Uses 4 buttons, A HScrollBar and A PictureBox.

Imports System.IO
Imports System.Drawing.Imaging
Imports System.Environment

Public Class Form1

    Private ActBMP As String
    Private FlipH As Boolean = False
    Private FlipV As Boolean = False
    Private RotVal As Byte = 0

    Private Enum MimeTypes
        PNG = 0
        JPEG = 1
        BMP = 2
    End Enum

    Private Class CodecData

        Public Sub New(MimeType As MimeTypes, iEncoderParams As EncoderParameters)

            'Get List Of Available Codecs
            Dim encoders() As ImageCodecInfo = ImageCodecInfo.GetImageEncoders

            'Get Codec For Specified Mime Type
            CodecInfo = encoders.First(Function(P As ImageCodecInfo) P.MimeType = "image/" & LCase(MimeType.ToString))

            EncoderParams = iEncoderParams

        End Sub

        Public Property CodecInfo As ImageCodecInfo
        Public Property EncoderParams As EncoderParameters

    End Class

    'Open Image Button
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim OFD As OpenFileDialog = New OpenFileDialog

        OFD.Multiselect = False
        OFD.Filter = "Png|*.png|Jpg|*.jpg|Bitmap|*.bmp|All Supported|*.png;*.jpg;*.bmp"

        If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then

            NewBitmap(OFD.FileName)

        End If

    End Sub

    Private Sub NewBitmap(ImagePath As String)

        hsQuality.Value = 100
        ActBMP = ImagePath
        Canvas.Image = New Bitmap(ActBMP)

    End Sub

    'Quality HScrollBar (Min\Max 1\100)
    Private Sub hsQuality_ValueChanged(sender As Object, e As EventArgs) Handles hsQuality.ValueChanged

        'Create a Parameters List With 1 Entry
        Dim EPs As Drawing.Imaging.EncoderParameters = New EncoderParameters(1)

        'Create A Parameter Which Adjusts The Quality
        Dim EP As EncoderParameter = New EncoderParameter(Encoder.Quality, hsQuality.Value)

        'Add The Parameter To The Parameters List
        EPs.Param(0) = EP

        'Store The Parameter Data in The CodecData Object
        Dim cData As New CodecData(MimeTypes.JPEG, EPs)

        UpdateImage(cData)

    End Sub


    Private Sub UpdateImage(cData As CodecData)

        If Trim(ActBMP) = "" Then Exit Sub

        'Load A New Instance Of The Bitmap
        Dim tmpBMP As New Bitmap(ActBMP)
        Dim MS As MemoryStream = New MemoryStream()

        If IsNothing(cData.EncoderParams) Then

            Canvas.Image = New Bitmap(ActBMP)

        Else
            'Save The Bitmap With The Applied Codec And Parameters To
            'A Memory Stream
            tmpBMP.Save(MS, cData.CodecInfo, cData.EncoderParams)
            tmpBMP.Dispose()

            'Load The Memory Stream Bitmap Into The PictureBox
            Canvas.Image = New Bitmap(MS)
        End If

        Canvas.Refresh()

        MS.Dispose()

    End Sub

    'Flip Hoz Button
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        hsQuality.Value = 100
        FlipH = Not (FlipH)

        Dim tmpBMP As New Bitmap(ActBMP)

        If FlipH = True Then
            'Basic Flip Horizontal
            tmpBMP.RotateFlip(RotateFlipType.RotateNoneFlipX)
        End If

        Canvas.Image = tmpBMP

    End Sub

    'Flip Vert Button
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

        hsQuality.Value = 100
        FlipV = Not (FlipV)

        Dim tmpBMP As New Bitmap(ActBMP)

        If FlipV = True Then
            'Basic Flip Vertically
            tmpBMP.RotateFlip(RotateFlipType.RotateNoneFlipY)
        End If

        Canvas.Image = tmpBMP

    End Sub

    'Rotate Button
    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click

        hsQuality.Value = 100
        RotVal += 1

        If RotVal > 3 Then RotVal = 0

        Dim tmpBMP As New Bitmap(ActBMP)

        'Basic Rotate
        tmpBMP.RotateFlip(RotateFlipType.RotateNoneFlipNone + RotVal)

        Canvas.Image = tmpBMP

        Canvas.Refresh()

    End Sub

End Class
Santanu.Das 125 Santanu Das

Fine work, mainly the maintenance of picture quality.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.