split an image programmatically

  • Thread starter Thread starter dineshbajaj
  • Start date Start date
D

dineshbajaj

Hi Everybody,

I need to split an image file(*.jpeg) programmatically to nine or more
equal parts.

Since, the code is meant to run on Windows Pocket PC 2002, I can only
use methods supported by the .Net Compact Framework.

Thanks in Advance,

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
 
first calculate the different x,y positions you want to get from the source
image, then the height and with of every piece and use g.drawimage

Dim g As Graphics
Dim x(8) As Bitmap
for i as integer = 0 to 8
x(i) = New Bitmap(90, 90)
g = Graphics.FromImage(x(i))
g.DrawImage(PictureBox1.Image, New Rectangle(0, 0, 90, 90), x, y, 90, 90, _
GraphicsUnit.Pixel)
next

hth
 
dineshbajaj,

Here is a function that splits an image into an array of smaller images. In you pass in rows = 2 and columns = 2 you will get back four images, each a quarter of the original image, each representing a different quadrant of the image.

Private Function SplitImage(ByVal wholeImage As Image, ByVal rows As Integer, _
ByVal columns As Integer) As Image()
Dim cellWidth As Single = CType(wholeImage.Width / columns, Single)
Dim cellHeight As Single = CType(wholeImage.Height / rows, Single)
' Create an array of RectangleF to hold the RectangleFs
' that define parts (cells) of the wholeImage.
Dim rectangleFs((rows * columns) - 1) As RectangleF
' Fill RectangleFs array with RectangleF objects.
Dim currentRow As Single = 0
Dim currentColumn As Single = 0
Dim rectangleFIndex As Integer = 0
For currentRow = 0 To rows - 1
For currentColumn = 0 To columns - 1
Dim newRectangleF As New RectangleF()
newRectangleF.X = currentColumn * cellWidth
newRectangleF.Y = currentRow * cellHeight
newRectangleF.Width = cellWidth
newRectangleF.Height = cellHeight
rectangleFs(rectangleFIndex) = newRectangleF
rectangleFIndex += 1
Next
Next
' Create an array of Images to contain the images
' that will be created from the wholeImage.
Dim images(rectangleFs.GetLength(0)) As Image
' Fill images array with Images.
Dim i As Integer = 0
For i = 0 To rectangleFs.GetUpperBound(0)
Dim newBitMap As New Bitmap(CType(cellWidth, Integer), _
CType(cellHeight, Integer))
Dim bitMapGraphics As Graphics = Graphics.FromImage(newBitMap)
bitMapGraphics.Clear(Color.Blue)
bitMapGraphics.DrawImage(wholeImage, newBitMap.GetBounds(GraphicsUnit.Pixel), _
rectangleFs(i), GraphicsUnit.Pixel)
images(i) = newBitMap
Next
Return images
End Function


--
Mike

Mike McIntyre
Visual Basic MVP
www.getdotnetcode.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top