Page 1 of 1

Ninja App: Quick and Dirty Image Degrading Rate Topic: -----

#1 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9201
  • View blog
  • Posts: 34,575
  • Joined: 12-June 08

Posted 03 May 2013 - 09:01 AM

Tools:
Visual Studio
.NET Framework 4.0
Vb.NET

Background:
A few years back I was messing around with some graphics for a website and found myself repeatedly trying to 'fuzz-out' or pixelate images for a certain effect. It was taking a whole mess of my time up as I was playing with various arrangements, and I decided to stream line the process with a small ninja app. Thus was born the 'image pixelator'!

The theory is quite simple and ingenious. If you have a given image and you shrink it down sufficiently the granularity (or resolution) of the pixels that make up a clear image are removed as they are repackaged in a smaller space. In that small space you can fiddle with quality, smoothing, etc. With the right combination this blocks up the pixels so when you re-size the image to regular size you can change the bits per pixel to something like 8bit and get a really fun effect. The smaller you crank the image the more degraded and blocky it looks.

Armed with that bit of for-planning it was a quick romp through the namespaces to cherry pick the various ways to change the image, how to re-size and image, and how to change the bits per pixel. See, a bit of planning illuminates the path to the solution!

Sort of the opposite of the tv show crime labs where the big boss is yelling "Select, zoom, ENHANCE!".

Attached Image

Public Class Form5

    Dim oImage As Image '-- original image

    ''' <summary>
    ''' A method to resize the image and apply image quality changes to block up the image.
    ''' </summary>
    Private Function MyResize(ByVal _image As Image, ByVal new_w As Int32, ByVal new_h As Int32) As Bitmap
        Dim ret As Bitmap = New Bitmap(new_w, new_h)

        Dim g As Graphics = Graphics.FromImage(ret)
        g.CompositingQuality = Drawing2D.CompositingQuality.Default
        g.InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor
        g.SmoothingMode = Drawing2D.SmoothingMode.None

        g.DrawImage(_image, 0, 0, ret.Width, ret.Height)

        g.Dispose()
        Return ret
    End Function

    ''' <summary>
    ''' change the original image by shrinking, formatting the pixels, and resizing (back to original) the futzed with image.
    ''' </summary>
    Private Sub trackbarQuality_ValueChanged(sender As Object, e As EventArgs) Handles trackbarQuality.ValueChanged
        Dim lVal As Int32 = 0 '-- The trackbar's values are the divisors for how small to shrink the image I apply the pixel formatting.
        Dim imgRehydrated As Image '-- after the image is shrunk, formatted, and expanded.
        Dim imgShrunk As Image '-- the image when the size is collapsed considerably.
        Dim imgAfterPixelFormating As Image '-- after the image is shrunk I can flip the pixel format to 8bpp through the bitmap namespace's clone.

        If pbModified.Image IsNot Nothing Then
            '-- I found these values most agreeable, but they can be tweaked at will.
            Select Case trackbarQuality.Value
                Case 0
                    lVal = 0
                Case 1
                    lVal = 1
                Case 2
                    lVal = 2
                Case 3
                    lVal = 3
                Case 4
                    lVal = 4
                Case 5
                    lVal = 8
            End Select

            '-- let's not divide by zero; instead use the original image.
            If lVal = 0 Then
                imgRehydrated = oImage
            Else
                '-- collapse the image on itself by how far the slider has been moved
                imgShrunk = MyResize(oImage, CInt(oImage.Width / lVal), CInt(oImage.Height / lVal))
                '-- change the current image format to 8bbp (gets a nice blocky style on the shrunk image)
                imgAfterPixelFormating = CType(imgShrunk, Bitmap).Clone(New Rectangle(0, 0, imgShrunk.Width, imgShrunk.Height), Imaging.PixelFormat.Format8bppIndexed)
                '-- bring the image back to the original size.
                imgRehydrated = MyResize(imgAfterPixelFormating, CInt(oImage.Width), CInt(oImage.Height))
            End If

            '-- set the picturebox to the new image
            pbModified.Image = Nothing
            pbModified.Image = imgRehydrated

            '-- implicitly call the garbage collector to keep the memory foot print down 
            GC.Collect()
        End If
    End Sub

    ''' <summary>
    ''' Open the 'open file dialog' to select an image to modify.
    ''' </summary>
    Private Sub tsbLoad_Click(sender As Object, e As EventArgs) Handles tsbLoad.Click
        If ofdFile.ShowDialog = Windows.Forms.DialogResult.OK Then
            uxTxtLocation.Text = ofdFile.FileName '-- I like knowing where my file is.

            trackbarQuality.Value = 0 '-- set the trackbar back to 0

            If IO.File.Exists(uxTxtLocation.Text) Then '-- verify it exists
                oImage = New Bitmap(uxTxtLocation.Text) '-- load the image
                pbOriginal.Image = oImage '-- set the original picture box to this image
                pbModified.Image = oImage '-- set the modifiedpicture box to this image
            Else
                MsgBox("""" + uxTxtLocation.Text + """ does not exist.  Try again.")
            End If

        End If
    End Sub

    ''' <summary>
    ''' Basic save for the modified image.
    ''' </summary>
    Private Sub tsbSave_Click(sender As Object, e As EventArgs) Handles tsbSave.Click
        sfDialog.FileName = String.Empty

        If sfDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            pbModified.Image.Save(sfDialog.FileName) '-- darn drawing namespace is helpful!
        End If

    End Sub

    ''' <summary>
    ''' Basic clear for all the usable objects.
    ''' </summary>
    Private Sub tsbClear_Click(sender As Object, e As EventArgs) Handles tsbClear.Click
        uxTxtLocation.Text = String.Empty
        ofdFile.FileName = String.Empty
        sfDialog.FileName = String.Empty
        pbOriginal.Image = Nothing
        pbModified.Image = Nothing
    End Sub
End Class



Designer:
Spoiler


A pretty fun utility app that I pull out now and then to impress folks on the turn around for pixelated images!


Extra reading:

http://msdn.microsof...ingquality.aspx
http://msdn.microsof...lationmode.aspx
http://msdn.microsof...y/z714w2y9.aspx
http://msdn.microsof...ixelformat.aspx

Is This A Good Question/Topic? 4
  • +

Replies To: Ninja App: Quick and Dirty Image Degrading

#2 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Posted 11 June 2013 - 10:58 AM

Excellent sir. I appreciate ya sharing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1