Friday, May 16, 2008
Image split in multiple part in asp.net
-------step-1------------
Put asp:button and file uploder tool in .aspx pages
-------Step-2------------
Importt two class in aspx.vb page
-------------------------
Imports System.Web.UI.WebControls.Unit
Imports System.Drawing
-------Step-3------------
Now On code behind on button click event write following
------------------------
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim obj As Image
obj = Drawing.Image.FromFile("C:\Documents and Settings\user2\Desktop\1m.jpg")
SplitImage(obj, 3, 1)
' fun1(obj, 3)
End Sub
-------Step-4------------
Now On code behind on Call Function(SplitImage()) write following
-------------------------
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 Drawing.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 Drawing.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 Drawing.Bitmap(CType(cellWidth, Integer), CType(cellHeight, Integer))
Dim bitMapGraphics As Drawing.Graphics = Graphics.FromImage(newBitMap)
bitMapGraphics.Clear(Color.Blue)
bitMapGraphics.DrawImage(wholeImage, newBitMap.GetBounds(GraphicsUnit.Pixel), rectangleFs(i), GraphicsUnit.Pixel)
images(i) = newBitMap
newBitMap.Save("C:\Documents and Settings\user2\Desktop\" + i.ToString() + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
Next
Return images
End Function
Put asp:button and file uploder tool in .aspx pages
-------Step-2------------
Importt two class in aspx.vb page
-------------------------
Imports System.Web.UI.WebControls.Unit
Imports System.Drawing
-------Step-3------------
Now On code behind on button click event write following
------------------------
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim obj As Image
obj = Drawing.Image.FromFile("C:\Documents and Settings\user2\Desktop\1m.jpg")
SplitImage(obj, 3, 1)
' fun1(obj, 3)
End Sub
-------Step-4------------
Now On code behind on Call Function(SplitImage()) write following
-------------------------
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 Drawing.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 Drawing.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 Drawing.Bitmap(CType(cellWidth, Integer), CType(cellHeight, Integer))
Dim bitMapGraphics As Drawing.Graphics = Graphics.FromImage(newBitMap)
bitMapGraphics.Clear(Color.Blue)
bitMapGraphics.DrawImage(wholeImage, newBitMap.GetBounds(GraphicsUnit.Pixel), rectangleFs(i), GraphicsUnit.Pixel)
images(i) = newBitMap
newBitMap.Save("C:\Documents and Settings\user2\Desktop\" + i.ToString() + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
Next
Return images
End Function
Subscribe to:
Posts (Atom)