Making Images 8-bit

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

49 Replies - 2776 Views - Last Post: 04 May 2013 - 05:58 AM Rate Topic: -----

#1 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Making Images 8-bit

Posted 23 April 2013 - 03:39 PM

So, I'm starting a little program that makes images "8-bit".I would usually do this through photoshop by resizing the image to 120x120, then changing it to Indexed color, 8 colors, and then resizing it to 500x500. I'm coming across 2 main problems:
1-How to resize the image, both times
2-How to change it to indexed color
So, could anyone help me out?
Also, my Visual Studio just went full retarded and it will write over my code, like this
Posted Image. How can i fix this?

Code for now:
Imports System.Drawing
Imports System.IO
Public Class Form1
    Dim IMG As Bitmap 
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Save.Enabled = False
    End Sub
    Private Sub File_Click(sender As Object, e As EventArgs) Handles File.Click
        Dim LOC As String
        OpenFile.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*"
        OpenFile.ShowDialog()
        LOC = OpenFile.FileName
        FileLoc.Text = LOC
        IMG = New Drawing.Bitmap(String.Format("{0}", LOC))
        Imgbox.Image = IMG
    End Sub

    Private Sub Preview_Click(sender As Object, e As EventArgs) Handles Preview.Click
        Save.Enabled = True
    End Sub

    Private Sub Save_Click(sender As Object, e As EventArgs) Handles Save.Click

    End Sub
    Private Sub FileLoc_TextChanged(sender As Object, e As EventArgs) Handles FileLoc.TextChanged

    End Sub
    Private Sub OpenFile_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFile.FileOk

    End Sub
End Class

This post has been edited by Raminator: 23 April 2013 - 03:41 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Making Images 8-bit

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: Making Images 8-bit

Posted 23 April 2013 - 03:49 PM

Press the Insert key to de-"retard", as you say.
Was This Post Helpful? 2
  • +
  • -

#3 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1030
  • View blog
  • Posts: 4,010
  • Joined: 02-July 08

Re: Making Images 8-bit

Posted 23 April 2013 - 03:51 PM

Are you sure you understand 'bits' as referred to images? They represent the histogram and density of the image not the size. Resizing as image is much more simple and do not require changing the 'bits'. So, with that, what are you after?
Was This Post Helpful? 0
  • +
  • -

#4 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: Making Images 8-bit

Posted 23 April 2013 - 03:56 PM

View PostAdamSpeight2008, on 23 April 2013 - 10:49 PM, said:

Press the Insert key to de-"retard", as you say.

Thanks a lot, its de-retarded now hahaha

View Post_HAWK_, on 23 April 2013 - 10:51 PM, said:

Are you sure you understand 'bits' as referred to images? They represent the histogram and density of the image not the size. Resizing as image is much more simple and do not require changing the 'bits'. So, with that, what are you after?

I do understand, thats why thats why i sad "8-Bit", i mean kind of a blocky image, with an oldschool look. Like this one i did using that photoshop tehnic Posted Image
Was This Post Helpful? 0
  • +
  • -

#5 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: Making Images 8-bit

Posted 23 April 2013 - 05:24 PM

Anyone?
Was This Post Helpful? 0
  • +
  • -

#6 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1030
  • View blog
  • Posts: 4,010
  • Joined: 02-July 08

Re: Making Images 8-bit

Posted 23 April 2013 - 09:17 PM

I was just clearing up what your wanted. The PixelFormat in the Image class tells it what format you want. One of the overloads for the Bitmap class has a parameter for this enumeration.
Was This Post Helpful? 1
  • +
  • -

#7 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: Making Images 8-bit

Posted 23 April 2013 - 09:23 PM

So, after some time of hard trying and testing I'm almost finished, i believe, but whi some big problems i need help fixing which are:
1 - HUGE use of memory (see attatchments)
2 - EXTREMELY confusing code
3 - Can't find a way of saving the the result of DOC operations (Line 83 won't work)
4 - Crash when "Cancel" is clicked on OpenFile.ShowDialog()
Code for now (requires Photoshop)
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Drawing2D
Imports System.IO
Imports Photoshop
Imports System.Security.Principal.WindowsIdentity
Public Class Form1
    Dim IMG As Bitmap
    Dim s(2) As Size
    Dim appref As Photoshop.Application
    Dim DOC As Photoshop.Document
    Dim LOC As String
    Dim USER As String = My.User.Name.Substring(My.User.Name.IndexOf("\") + 1)
    Dim DPATH As String = String.Format("C:\Users\{0}\Desktop\8-Bit\Artwork", USER)
    Dim RES As Double
    Dim TPATH As String
    Dim FileName As String
    Dim FPATH As String = String.Format("{0}/8BITRES", Path.GetTempPath)
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        checkfolder(DPATH)
        checkfolder(FPATH)
        Save.Enabled = False
        Preview.Enabled = False
        appref = New Application
        appref.Visible = False
        appref.DisplayDialogs = PsDialogModes.psDisplayNoDialogs
        s(0) = New Size(500, 500)
        s(1) = New Size(120, 120)
        OpenFile.AutoUpgradeEnabled = True
        OpenFile.Multiselect = False
        OpenFile.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF;*PNG;|All files (*.*)|*.*"
    End Sub
    Private Sub Form1_Exit(sender As Object, e As EventArgs) Handles MyBase.FormClosed
        appref.Quit()
    End Sub
    Private Sub File_Click(sender As Object, e As EventArgs) Handles File.Click
        OpenFile.ShowDialog()
        If (String.IsNullOrEmpty(OpenFile.FileName)) Then
        Else
            If (OpenFile.CheckFileExists = True) Then
                Preview.Enabled = True
                LOC = OpenFile.FileName
                FileLoc.Text = LOC
                IMG = New Drawing.Bitmap(String.Format("{0}", LOC))
                Imgbox.Image = ResizeImage(IMG, s(0), True)
            Else
                MessageBox.Show("File does not exist")
            End If
        End If       
    End Sub

    Private Sub Preview_Click(sender As Object, e As EventArgs) Handles Preview.Click
        Save.Enabled = True
        Dim EXT As New Photoshop.PsExtensionType
        EXT = 2
        Dim TMPIMG As Bitmap
        Dim jpeg As New Photoshop.JPEGSaveOptions
        jpeg.Quality = 8
        Dim MET As Photoshop.PsResampleMethod
        MET = PsResampleMethod.psBilinear
        Dim OPTS As New Photoshop.PNGSaveOptions
        OPTS.Compression = 8
        Dim CLRPT As New Photoshop.IndexedConversionOptions
        CLRPT.Colors = 8
        CLRPT.Palette = PsPaletteType.psLocalPerceptual
        CLRPT.Dither = PsDitherType.psDiffusion
        CLRPT.DitherAmount = 75
        CLRPT.Transparency = True
        CLRPT.PreserveExactColors = True
        CLRPT.Forced = PsForcedColors.psNoForced
        Dim FN As Uri
        FN = New Uri(LOC)
        Dim FN2 As String() = FN.Segments
        FileName = FN2(FN2.Length - 1)
        TPATH = String.Format("{0}/{1}.JPG", Path.GetTempPath, FileName)
        TMPIMG = IMG
        TMPIMG.Save(TPATH, System.Drawing.Imaging.ImageFormat.Jpeg)
        DOC = appref.Open(TPATH)
        RES = DOC.Resolution
        DOC.ResizeImage(120, 120, RES, MET)
        DOC.ChangeMode(PsChangeMode.psConvertToIndexedColor, CLRPT)
        DOC.ResizeImage(120, 120, RES, MET)
        DOC.SaveAs(String.Format("{0}/{1}.JPG", FPATH, FileName), jpeg, False, EXT)
        Imgbox.Image = Image.FromFile(String.Format("{0}/{1}.JPG", FPATH, FileName))

    End Sub

    Private Sub Save_Click(sender As Object, e As EventArgs) Handles Save.Click
        Dim FIMG As Bitmap
        FIMG = Image.FromFile(TPATH)
        FIMG.Save(String.Format("{0}/{1}.JPG", DPATH, FileName), Imaging.ImageFormat.Jpeg)

    End Sub
    Private Sub FileLoc_TextChanged(sender As Object, e As EventArgs) Handles FileLoc.TextChanged

    End Sub
    Private Sub OpenFile_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFile.FileOk

    End Sub
    Public Shared Function ResizeImage(ByVal image As Image, ByVal size As Size, Optional ByVal preserveAspectRatio As Boolean = True) As Image
        Dim newWidth As Integer
        Dim newHeight As Integer
        If preserveAspectRatio Then
            Dim originalWidth As Integer = image.Width
            Dim originalHeight As Integer = image.Height
            Dim percentWidth As Single = CSng(Size.Width) / CSng(originalWidth)
            Dim percentHeight As Single = CSng(Size.Height) / CSng(originalHeight)
            Dim percent As Single = If(percentHeight < percentWidth, percentHeight, percentWidth)
            newWidth = CInt(originalWidth * percent)
            newHeight = CInt(originalHeight * percent)
        Else
            newWidth = Size.Width
            newHeight = Size.Height
        End If
        Dim newImage As Image = New Bitmap(newWidth, newHeight)
        Using graphicsHandle As Graphics = Graphics.FromImage(newImage)
            graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic
            graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight)
        End Using
        Return newImage
    End Function
    Private Sub checkfolder(ByVal direct As String)
        If Not Directory.Exists(direct) Then
            Directory.CreateDirectory(direct)
        Else
        End If
    End Sub
End Class

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#8 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: Making Images 8-bit

Posted 24 April 2013 - 07:18 AM

View Post_HAWK_, on 24 April 2013 - 04:17 AM, said:

I was just clearing up what your wanted. The PixelFormat in the Image class tells it what format you want. One of the overloads for the Bitmap class has a parameter for this enumeration.

Ok, ill work on trying to use that, also, i did some minor changes to my code so it doesnt look so horrible, but bugs remain in there.
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Drawing2D
Imports System.IO
Imports Photoshop
Imports System.Security.Principal.WindowsIdentity
Public Class Form1
#Region "GVAR"
    Dim IMG As Bitmap
    Dim s(2) As Size
    Dim appref As Photoshop.Application
    Dim DOC As Photoshop.Document
    Dim LOC As String
    Dim USER As String = My.User.Name.Substring(My.User.Name.IndexOf("\") + 1)
    Dim DPATH As String = String.Format("C:\Users\{0}\Desktop\8-Bit\Artwork", USER)
    Dim RES As Double
    Dim TPATH As String
    Dim FileName As String
    Dim FPATH As String = String.Format("{0}/8BITRES", Path.GetTempPath)
    Dim EXT As New Photoshop.PsExtensionType
    Dim jpeg As New Photoshop.JPEGSaveOptions
    Dim MET As Photoshop.PsResampleMethod
    Dim OPTS As New Photoshop.PNGSaveOptions
    Dim CLRPT As New Photoshop.IndexedConversionOptions
#End Region
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        checkfolder(DPATH)
        checkfolder(FPATH)
        Save.Enabled = False
        Preview.Enabled = False
        appref.Visible = False
        appref = New Application
        appref.DisplayDialogs = PsDialogModes.psDisplayNoDialogs
        s(0) = New Size(500, 500)
        s(1) = New Size(120, 120)
        OpenFile.AutoUpgradeEnabled = True
        OpenFile.Multiselect = False
        OpenFile.Filter = "Image Files|*.BMP;*.JPG;*.GIF;*PNG;|All files (*.*)|*.*"
    End Sub
    Private Sub Form1_Exit(sender As Object, e As EventArgs) Handles MyBase.FormClosed
        appref.Quit()
    End Sub
    Private Sub File_Click(sender As Object, e As EventArgs) Handles File.Click
        OpenFile.ShowDialog()
        If (String.IsNullOrEmpty(OpenFile.FileName)) Then
        Else
            If (OpenFile.CheckFileExists = True) Then
                Preview.Enabled = True
                LOC = OpenFile.FileName
                FileLoc.Text = LOC
                IMG = New Drawing.Bitmap(String.Format("{0}", LOC))
                Imgbox.Image = ResizeImage(IMG, s(0), False)
            Else
                MessageBox.Show("File does not exist")
            End If
        End If
    End Sub

    Private Sub Preview_Click(sender As Object, e As EventArgs) Handles Preview.Click
        Save.Enabled = True
        Dim TMPIMG As Bitmap
        SETOPTS()
        FileName = GETSEG(LOC)
        TPATH = String.Format("{0}/{1}.JPG", Path.GetTempPath, FileName)
        TMPIMG = IMG
        TMPIMG.Save(TPATH, System.Drawing.Imaging.ImageFormat.Jpeg)
        DOC = appref.Open(TPATH)
        RES = DOC.Resolution
        DOC.ResizeImage(120, 120, RES, MET)
        DOC.ChangeMode(PsChangeMode.psConvertToIndexedColor, CLRPT)
        DOC.ResizeImage(120, 120, RES, MET)
        DOC.SaveAs(String.Format("{0}/{1}.JPG", FPATH, FileName), jpeg, False, EXT)
        Imgbox.Image = Image.FromFile(String.Format("{0}/{1}.JPG", FPATH, FileName))
    End Sub
    Private Sub Save_Click(sender As Object, e As EventArgs) Handles Save.Click
        Dim FIMG As Bitmap
        FIMG = Image.FromFile(TPATH)
        FIMG.Save(String.Format("{0}/{1}.JPG", DPATH, FileName), Imaging.ImageFormat.Jpeg)
    End Sub
    Public Shared Function ResizeImage(ByVal image As Image, ByVal size As Size, Optional ByVal preserveAspectRatio As Boolean = True) As Image
        Dim newWidth As Integer
        Dim newHeight As Integer
        If preserveAspectRatio Then
            Dim originalWidth As Integer = image.Width
            Dim originalHeight As Integer = image.Height
            Dim percentWidth As Single = CSng(size.Width) / CSng(originalWidth)
            Dim percentHeight As Single = CSng(size.Height) / CSng(originalHeight)
            Dim percent As Single = If(percentHeight < percentWidth, percentHeight, percentWidth)
            newWidth = CInt(originalWidth * percent)
            newHeight = CInt(originalHeight * percent)
        Else
            newWidth = size.Width
            newHeight = size.Height
        End If
        Dim newImage As Image = New Bitmap(newWidth, newHeight)
        Using graphicsHandle As Graphics = Graphics.FromImage(newImage)
            graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic
            graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight)
        End Using
        Return newImage
    End Function
    Function checkfolder(ByVal direct As String)
        If Not Directory.Exists(direct) Then
            Directory.CreateDirectory(direct)
        Else
        End If
    End Function
    Function SETOPTS()
        EXT = 2
        jpeg.Quality = 8
        MET = PsResampleMethod.psBilinear
        OPTS.Compression = 8
        CLRPT.Colors = 8
        CLRPT.Palette = PsPaletteType.psLocalPerceptual
        CLRPT.Dither = PsDitherType.psDiffusion
        CLRPT.DitherAmount = 75
        CLRPT.Transparency = True
        CLRPT.PreserveExactColors = True
        CLRPT.Forced = PsForcedColors.psNoForced
    End Function
    Function GETSEG(ByVal STR As String)
        Dim Fn As Uri
        Fn = New Uri(STR)
        Dim FN2 As String() = Fn.Segments
        Return (FN2(FN2.Length - 1))
    End Function
End Class


Was This Post Helpful? 0
  • +
  • -

#9 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: Making Images 8-bit

Posted 24 April 2013 - 07:44 AM

View Post_HAWK_, on 24 April 2013 - 04:17 AM, said:

I was just clearing up what your wanted. The PixelFormat in the Image class tells it what format you want. One of the overloads for the Bitmap class has a parameter for this enumeration.

Ok, so i started to use that method and, for example,
Dim IMG120 as Bitmap
but
IMG120.PixelFormat
is a read only property, i can't change it. Is it possible to change it somehow?

This post has been edited by Raminator: 24 April 2013 - 07:45 AM

Was This Post Helpful? 0
  • +
  • -

#10 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8374
  • View blog
  • Posts: 31,122
  • Joined: 12-June 08

Re: Making Images 8-bit

Posted 24 April 2013 - 07:58 AM

Yeah.. good ol' clone!
Was This Post Helpful? 1
  • +
  • -

#11 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: Making Images 8-bit

Posted 24 April 2013 - 08:09 AM

View Postmodi123_1, on 24 April 2013 - 02:58 PM, said:

Yeah.. good ol' clone!

sorry?
Was This Post Helpful? 0
  • +
  • -

#12 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8374
  • View blog
  • Posts: 31,122
  • Joined: 12-June 08

Re: Making Images 8-bit

Posted 24 April 2013 - 08:20 AM

The bitmap namespace has a method called 'clone'.. you can turn flip the image bit quality there.

After some futzing I set up my image erosion on a slider!

Posted Image
Was This Post Helpful? 1
  • +
  • -

#13 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: Making Images 8-bit

Posted 24 April 2013 - 08:26 AM

View Postmodi123_1, on 24 April 2013 - 03:20 PM, said:

The bitmap namespace has a method called 'clone'.. you can turn flip the image bit quality there.

After some futzing I set up my image erosion on a slider!

Posted Image

Ok, i found the Clone Method but where would i change the image bit quality? Also, nice program :)
Was This Post Helpful? 0
  • +
  • -

#14 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: Making Images 8-bit

Posted 24 April 2013 - 10:47 AM

View PostRaminator, on 24 April 2013 - 03:26 PM, said:

View Postmodi123_1, on 24 April 2013 - 03:20 PM, said:

The bitmap namespace has a method called 'clone'.. you can turn flip the image bit quality there.

After some futzing I set up my image erosion on a slider!

Posted Image

Ok, i found the Clone Method but where would i change the image bit quality? Also, nice program :)/>

modi123_1 ?
Was This Post Helpful? 0
  • +
  • -

#15 Raminator  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 283
  • Joined: 16-July 12

Re: Making Images 8-bit

Posted 24 April 2013 - 04:46 PM

anyone, please
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »