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

how to divide a 800x600 image in 32x32 pixels files?

i need to create a program that opens an 800x600 or 1024x768 image file (bmp or png) and divide it into 32x32 or 64x64 pixels images.

i did this with the pictureclip control in vb 6 a year a go, now in vb.net it is very confusing to me and i this is my first "solution" with vb.net.

can somebody help me with this question?

thanks!
0
José Perez
Asked:
José Perez
  • 8
  • 5
  • 3
  • +3
1 Solution
 
José PerezAuthor Commented:
there is no answer i can use!

maybe you write the wrong link?

maybe i need some equivalent control to PictureClip in VB.net, please any idea may help.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You can use code like this to draw a portion of an image into a new bitmap and save it:

        Dim sourceImage As Bitmap = Image.FromFile("c:\someImage.bmp")

        Dim tile As New Bitmap(32, 32)
        Dim g As Graphics = Graphics.FromImage(tile)
        Dim sourceRect As New Rectangle(0, 0, 32, 32)
        g.DrawImage(sourceImage, 0, 0, sourceRect, GraphicsUnit.Pixel)
        g.Dispose()
        tile.Save("c:\Tile1.bmp")

So you can change the location of the sourceRect and the name of the saved tile based on a loop.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Carl TawnSystems and Integration DeveloperCommented:
This will chop an 800x800 BMP into 32x32 slices and save them. You can easily modify it to cater for other image sizes:

            const int SLICE_SIZE = 32;

            Bitmap src = new Bitmap("C:\\Test.bmp");
            int file = 1;

            for (int y = 0; y < src.Height; y += SLICE_SIZE)
            {
                for (int x = 0; x < src.Width; x += SLICE_SIZE)
                {
                    Point p = new Point(x, y);

                    Bitmap bmp = src.Clone(new Rectangle(p, new Size(SLICE_SIZE, SLICE_SIZE)), src.PixelFormat);
                    bmp.Save("C:\\Slice" + (file++).ToString() + ".bmp", src.RawFormat);
                }
            }
0
 
José PerezAuthor Commented:
none of this code work it because i just paste it in button code, i think i need more information, do i have to add a reference, component or an special control to the current solution?
0
 
Carl TawnSystems and Integration DeveloperCommented:
Just realized that I posted C# code into a VB.Net question :o)  I can translate if you need it.

The only thing you need for my sample is to import the System.Drawing namespace. You will also need to add the reference to the assembly if it is not already included.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
No references or components are needed here...

You need to have some kind of source image.  In our examples we are loading one at run-time via code.

Do you want to load your source image via code or at design-time in a PictureBox for instance?
0
 
Carl TawnSystems and Integration DeveloperCommented:
FYI, here is a VB.Net version of my earlier sample:

        Const SLICE_SIZE As Integer = 32

        Dim src As New Bitmap("C:\Test.bmp")
        Dim file As Integer = 1

        Dim y As Integer = 0, x As Integer = 0

        While y < src.Height
            While x < src.Width

                Dim p As New Point(x, y)
                Dim bmp As Bitmap = src.Clone(New Rectangle(p, New Size(SLICE_SIZE, SLICE_SIZE)), src.PixelFormat)
                bmp.Save("C:\Slice" + file.ToString() + ".bmp", src.RawFormat)

                file += 1
                x += 1

            End While
            y += 1
        End While
0
 
Carl TawnSystems and Integration DeveloperCommented:
Slight correction:

        Const SLICE_SIZE As Integer = 32

        Dim src As New Bitmap("C:\Test.bmp")
        Dim file As Integer = 1

        Dim y As Integer = 0, x As Integer = 0

        While y < src.Height
            While x < src.Width

                Dim p As New Point(x, y)
                Dim bmp As Bitmap = src.Clone(New Rectangle(p, New Size(SLICE_SIZE, SLICE_SIZE)), src.PixelFormat)
                bmp.Save("C:\Slice" + file.ToString() + ".bmp", src.RawFormat)

                file += 1
                x += SLICE_SIZE

            End While
            y += SLICE_SIZE
        End While
0
 
José PerezAuthor Commented:
sorry this stupid question... how do i import de System.Drawing namespace? i tried as i supposed but it failed :(
0
 
Carl TawnSystems and Integration DeveloperCommented:
At the top of your class, but outside of the class, type "Imports System.Drawing".

If you get an error saying its not recognised, then go to Project > Add Reference, the scroll down to System.Drawing.dll
0
 
Bob LearnedCommented:
It is still not right, but here's an attempt:

Public Class PictureClip

  Public Sub New(ByVal fileName As String)

    m_image = Image.FromFile(fileName)

  End Sub

  Private m_image As Image

  Private m_clipX As Integer = 0
  Public Property ClipX() As Integer
    Get
      Return m_clipX
    End Get
    Set(ByVal Value As Integer)
      m_clipX = Value
    End Set
  End Property

  Private m_clipY As Integer = 0
  Public Property ClipY() As Integer
    Get
      Return m_clipY
    End Get
    Set(ByVal Value As Integer)
      m_clipY = Value
    End Set
  End Property

  Private m_clipWidth As Integer = 0
  Public Property ClipWidth() As Integer
    Get
      Return m_clipWidth
    End Get
    Set(ByVal Value As Integer)
      m_clipWidth = Value
    End Set
  End Property

  Private m_clipHeight As Integer = 0
  Public Property ClipHeight() As Integer
    Get
      Return m_clipHeight
    End Get
    Set(ByVal Value As Integer)
      m_clipHeight = Value
    End Set
  End Property

  Public ReadOnly Property Cols() As Integer
    Get
      Return m_image.Width \ Me.ClipWidth
    End Get
  End Property

  Public ReadOnly Property Rows() As Integer
    Get
      Return m_image.Height \ Me.ClipHeight
    End Get
  End Property

  Public ReadOnly Property Clip() As Image
    Get
      Dim sourceRect As Rectangle = New Rectangle(Me.ClipX, Me.ClipY, Me.ClipWidth, Me.ClipHeight)

      Return Me.GetImage(sourceRect)
    End Get
  End Property

  Public ReadOnly Property Cells() As Integer
    Get
      Return Me.Rows * Me.Cols
    End Get
  End Property

  Public ReadOnly Property GraphicsCell(ByVal index As Integer) As Image
    Get
      Dim row As Integer = index \ Me.Rows
      Dim col As Integer = index Mod Me.Cols

      Dim sourceRect As Rectangle = New Rectangle(col * Me.ClipWidth, row * Me.ClipHeight, Me.ClipWidth, Me.ClipHeight)

      Return GetImage(sourceRect)
    End Get
  End Property

  Private Function GetImage(ByVal sourceRect As Rectangle) As Image

    If Me.ClipWidth <= 0 OrElse Me.ClipWidth > m_image.Width Then
      Throw New ArgumentException("Invalid clip dimensions", "ClipWidth")
    End If

    If Me.ClipHeight <= 0 OrElse Me.ClipHeight > m_image.Height Then
      Throw New ArgumentException("Invalid clip dimensions", "ClipHeight")
    End If

    Dim clipImage As New Bitmap(Me.ClipWidth, Me.ClipHeight)
    Dim g As Graphics = Graphics.FromImage(clipImage)
    g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
    g.DrawImage(m_image, 0, 0, sourceRect, GraphicsUnit.Pixel)

    g.Dispose()

    Return clipImage

  End Function

End Class
0
 
Bob LearnedCommented:
Test form:

Public Class Form1
  Inherits System.Windows.Forms.Form

#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 Button1 As System.Windows.Forms.Button
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    Me.Button1 = New System.Windows.Forms.Button
    Me.SuspendLayout()
    '
    'Button1
    '
    Me.Button1.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
    Me.Button1.Location = New System.Drawing.Point(504, 16)
    Me.Button1.Name = "Button1"
    Me.Button1.TabIndex = 1
    Me.Button1.Text = "Button1"
    '
    'Form1
    '
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.BackColor = System.Drawing.SystemColors.Control
    Me.ClientSize = New System.Drawing.Size(592, 362)
    Me.Controls.Add(Me.Button1)
    Me.KeyPreview = True
    Me.Name = "Form1"
    Me.Text = "Form1"
    Me.WindowState = System.Windows.Forms.FormWindowState.Maximized
    Me.ResumeLayout(False)

  End Sub

#End Region

  Dim picClip1 As New PictureClip("C:\WINDOWS\Web\Wallpaper\Wind.jpg")


  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    picClip1.ClipWidth = 32
    picClip1.ClipHeight = 32

    Dim x, y As Integer
    Dim col As Integer = 0
    For cell As Integer = 0 To picClip1.Cells - 1
      Dim pict As New PictureBox
      pict.SizeMode = PictureBoxSizeMode.AutoSize
      pict.Image = picClip1.GraphicsCell(cell)
      pict.Location = New Point(x, y)
      x += 33
      col += 1

      If col >= picClip1.Cols Then
        col = 0
        x = 0
        y += 33
      End If

      Me.Controls.Add(pict)
    Next cell

  End Sub
End Class
0
 
José PerezAuthor Commented:
it says

error BC30289: Statement cannot appear within a method body. End of method assumed.

that error is displayed in "End Class"

see the code:

Imports System.Drawing
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Const SLICE_SIZE As Integer = 32

        Dim src As New Bitmap("C:\Test.bmp")
        Dim file As Integer = 1

        Dim y As Integer = 0, x As Integer = 0

        While y < src.Height
            While x < src.Width

                Dim p As New Point(x, y)
                Dim bmp As Bitmap = src.Clone(New Rectangle(p, New Size(SLICE_SIZE, SLICE_SIZE)), src.PixelFormat)
                bmp.Save("C:\Slice" + file.ToString() + ".bmp", src.RawFormat)

                file += 1
                x += SLICE_SIZE

            End While
            y += SLICE_SIZE
        End While
End Class
0
 
Bob LearnedCommented:
Your missing 'End Sub' after 'End While'.

Bob
0
 
Carl TawnSystems and Integration DeveloperCommented:
You appear to be missing your "End Sub". It should be between the last End While and End Class

i.e.

     y += SLICE_SIZE
   End While
  End Sub
End Class
0
 
José PerezAuthor Commented:
carl, now it says:

A first chance exception of type 'System.OutOfMemoryException' occurred in System.Drawing.dll
An unhandled exception of type 'System.OutOfMemoryException' occurred in System.Drawing.dll

Additional information: Out of memory.
0
 
Carl TawnSystems and Integration DeveloperCommented:
Change these two lines:

        While y < src.Height
            While x < src.Width

To:

        While y <= src.Height
            While x <= src.Width
0
 
Carl TawnSystems and Integration DeveloperCommented:
Ignore that last post. The code should look like:

        While y < src.Height
            While x < src.Width

                Dim p As New Point(x, y)
                Dim bmp As Bitmap = src.Clone(New Rectangle(p, New Size(SLICE_SIZE, SLICE_SIZE)), src.PixelFormat)
                bmp.Save("C:\Slice" + file.ToString() + ".bmp", src.RawFormat)
                bmp.Dispose()

                file += 1
                x += SLICE_SIZE

            End While
            x = 0
            y += SLICE_SIZE
        End While

I have tested this properly, and it should be ok. It should slice an 800x800 image into 625 32x32 parts.

What size is the image you are using ?
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 8
  • 5
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now