José Perez
asked on
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!
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!
https://www.experts-exchange.com/questions/21728136/Splitting-up-an-image.html
ASKER
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.
maybe you write the wrong link?
maybe i need some equivalent control to PictureClip in VB.net, please any idea may help.
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:\someIma ge.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.
Dim sourceImage As Bitmap = Image.FromFile("c:\someIma
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.
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);
}
}
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);
}
}
ASKER
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?
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.
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.
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?
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?
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
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
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
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
ASKER
sorry this stupid question... how do i import de System.Drawing namespace? i tried as i supposed but it failed :(
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
If you get an error saying its not recognised, then go to Project > Add Reference, the scroll down to System.Drawing.dll
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(clipIma ge)
g.SmoothingMode = Drawing2D.SmoothingMode.Hi ghQuality
g.DrawImage(m_image, 0, 0, sourceRect, GraphicsUnit.Pixel)
g.Dispose()
Return clipImage
End Function
End Class
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
End If
If Me.ClipHeight <= 0 OrElse Me.ClipHeight > m_image.Height Then
Throw New ArgumentException("Invalid
End If
Dim clipImage As New Bitmap(Me.ClipWidth, Me.ClipHeight)
Dim g As Graphics = Graphics.FromImage(clipIma
g.SmoothingMode = Drawing2D.SmoothingMode.Hi
g.DrawImage(m_image, 0, 0, sourceRect, GraphicsUnit.Pixel)
g.Dispose()
Return clipImage
End Function
End Class
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.ICon tainer
'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.Butto n
<System.Diagnostics.Debugg erStepThro ugh()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Butto n
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Anchor = CType((System.Windows.Form s.AnchorSt yles.Top Or System.Windows.Forms.Ancho rStyles.Ri ght), System.Windows.Forms.Ancho rStyles)
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.SystemColor s.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.FormW indowState .Maximized
Me.ResumeLayout(False)
End Sub
#End Region
Dim picClip1 As New PictureClip("C:\WINDOWS\We b\Wallpape r\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.AutoSiz e
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
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.ICon
'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.Butto
<System.Diagnostics.Debugg
Me.Button1 = New System.Windows.Forms.Butto
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Anchor = CType((System.Windows.Form
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.SystemColor
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.FormW
Me.ResumeLayout(False)
End Sub
#End Region
Dim picClip1 As New PictureClip("C:\WINDOWS\We
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.AutoSiz
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
ASKER
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
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
Your missing 'End Sub' after 'End While'.
Bob
Bob
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
i.e.
y += SLICE_SIZE
End While
End Sub
End Class
ASKER
carl, now it says:
A first chance exception of type 'System.OutOfMemoryExcepti on' occurred in System.Drawing.dll
An unhandled exception of type 'System.OutOfMemoryExcepti on' occurred in System.Drawing.dll
Additional information: Out of memory.
A first chance exception of type 'System.OutOfMemoryExcepti
An unhandled exception of type 'System.OutOfMemoryExcepti
Additional information: Out of memory.
Change these two lines:
While y < src.Height
While x < src.Width
To:
While y <= src.Height
While x <= src.Width
While y < src.Height
While x < src.Width
To:
While y <= src.Height
While x <= src.Width
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi check this
it may Help you
http://www.codeproject.com/aspnet/pnguploader.asp
Reference Link :
https://www.experts-exchange.com/questions/21771147/Uploading-and-resizing-picture.html
Thanks
it may Help you
http://www.codeproject.com/aspnet/pnguploader.asp
Reference Link :
https://www.experts-exchange.com/questions/21771147/Uploading-and-resizing-picture.html
Thanks