We help IT Professionals succeed at work.

VB.NET Split image

bolox2
bolox2 used Ask the Experts™
on
I have a JPG image of X by Y.

If the height of the image is greater than (for example) 700 then to split it into an another image,
Therefore if the image is 1400 in height, there would be 2 new images of 700 high

(This would of course be anything up to 5 images)

IF anyone can help this would be great, in fact I will give you loads of points if you put this as complete as possible :)
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2015

Commented:
Call this class with your image path and the number of splits you want like this:

        Dim splitImg As New SplitImage()
        splitImg.SplitImage("C:\temp\Images\myImage.jpg", 3)

And it will split it and save the new images to the folder you create and specify in line 42 of the code.
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Graphics

Public Class SplitImage

    Dim sideDimension As Integer

    Public Sub SplitImage(ByVal curFile As String, ByVal splits As Integer)
        Dim oldW, oldH As Integer
        Dim imgFileName As String = ""
        imgFileName = System.IO.Path.GetFileName(curFile)
        imgFileName = imgFileName.Substring(0, imgFileName.LastIndexOf("."))
        Dim curImg As System.Drawing.Image = System.Drawing.Image.FromFile(curFile)
        Dim Info As System.Drawing.Imaging.ImageCodecInfo() = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders
        Dim Params As System.Drawing.Imaging.EncoderParameters = New System.Drawing.Imaging.EncoderParameters(1)
        Params.Param(0) = New EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 80) 'Adjust jpeg compression amount here
        oldW = curImg.Width
        oldH = curImg.Height
        Dim fnlW As Integer = oldW
        Dim fnlH As Integer = oldH
        If oldH > oldW Then  'Image is taller than it is wide, vertical image
            fnlH = oldH / splits
            If fnlH * splits > oldH Then
                fnlH = fnlH - 1
            End If
        ElseIf oldW > oldH Then 'Image is wider than it is tall, horizontal image
            fnlW = oldW / splits
            If fnlW * splits > oldW Then
                fnlW = fnlW - 1
            End If
        End If
        Dim newImg As Bitmap = New Bitmap(curFile)
        Dim rec As New Rectangle(0, 0, fnlW, fnlH)
        For i As Integer = 0 To splits - 1
            If oldH > oldW Then
                rec = New Rectangle(0, fnlH * i, fnlW, fnlH)
            ElseIf oldW > oldH Then
                rec = New Rectangle(fnlW * i, 0, fnlW, fnlH)
            End If
            Dim splitBmp As Bitmap = newImg.Clone(rec, newImg.PixelFormat)
            splitBmp.Save("C:\temp\SplitImages\" & imgFileName & "_" & i & ".jpg", Info(1), Params)
        Next
    End Sub

End Class

Open in new window

you mean that you need to split an image in vb.net?
Top Expert 2015
Commented:
Here's another sub to add to the class I posted earlier. This one takes a path to an image and an integer representing the maximum pixels on a side for the split images. It will split in both directions if necessary as many times as necessary to cut images into squares of the size specified.

Call this class with your image path and the number of pixels you want on the longest side:

        Dim splitImg As New SplitImage()
        splitImg.SplitImage2("C:\temp\Images\myImage.jpg", 700)

And it will dice it up and save the new images to the folder you create and specify in the code.
Public Sub SplitImage2(ByVal curFile As String, ByVal maxDim As Integer)
        Dim oldW, oldH As Integer
        Dim imgFileName As String = ""
        imgFileName = System.IO.Path.GetFileName(curFile)
        imgFileName = imgFileName.Substring(0, imgFileName.LastIndexOf("."))
        Dim curImg As System.Drawing.Image = System.Drawing.Image.FromFile(curFile)
        Dim Info As System.Drawing.Imaging.ImageCodecInfo() = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders
        Dim Params As System.Drawing.Imaging.EncoderParameters = New System.Drawing.Imaging.EncoderParameters(1)
        Params.Param(0) = New EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 80) 'Adjust jpeg compression amount here
        oldW = curImg.Width
        oldH = curImg.Height
        Dim splitsH As Integer = 0
        Dim splitsW As Integer = 0
        If oldH > maxDim Then
            splitsH = oldH / maxDim
        End If
        If oldW > maxDim Then
            splitsW = oldW / maxDim
        End If
        Dim rec As New Rectangle(0, 0, oldW, oldH)
        Dim newImg As Bitmap = New Bitmap(curFile)
        Dim splitBmps As New ArrayList
        Dim splitBmpsH(splitsH - 1) As Bitmap
        If oldH > maxDim Then
            For h As Integer = 1 To splitsH
                Dim yPosition As Integer = oldH - (maxDim * h)
                If yPosition < 0 Then yPosition = 0
                Dim newHeight As Integer = maxDim * h
                If oldH - (maxDim * (h - 1)) < maxDim Then newHeight = oldH Mod maxDim
                If newHeight > 700 Then newHeight = 700
                rec = New Rectangle(0, yPosition, oldW, newHeight)
                Dim splitBmp As Bitmap = newImg.Clone(rec, newImg.PixelFormat)
                splitBmpsH(h - 1) = splitBmp
            Next
        End If
        If oldW > maxDim Then
            For h As Integer = 0 To splitsH - 1
                For w As Integer = 1 To splitsW
                    Dim xPosition As Integer = oldW - (maxDim * w)
                    If xPosition < 0 Then xPosition = 0
                    Dim newWidth As Integer = maxDim * w
                    If oldW - (maxDim * (w - 1)) < maxDim Then newWidth = oldW Mod maxDim
                    If newWidth > 700 Then newWidth = 700
                    rec = New Rectangle(xPosition, 0, newWidth, splitBmpsH(h).Height)
                    Dim splitBmp As Bitmap = splitBmpsH(h).Clone(rec, splitBmpsH(h).PixelFormat)
                    splitBmps.Add(splitBmp)
                Next
            Next
        End If
        For i As Integer = 0 To splitBmps.Count - 1
            splitBmps(i).Save("C:\temp\SplitImages\" & imgFileName & "_" & i & ".jpg", Info(1), Params)
        Next
    End Sub

Open in new window

Author

Commented:
Sorry for the delay. Your absolutely spot on. Thankyou very much for your full response. How do I increase my points on offer?
Top Expert 2015

Commented:
Your welcome and thank you for the maximum points.
Top Expert 2015

Commented:
I just looked at the code I posted. I noticed that I had hard coded the 700 number in for testing purposes. You will need to replace 700 with the variable maxDim.