sharunkumar 0 Newbie Poster

Hi everyone,

I just got this code which splits an image based on a 5x5 gride and saves them as smaller images...

This works perfectly for images less than 200x200 dimention but when i use a bigger file, I'm getting an "Out of memory" exception. Can someone help me out with this? Here's the code:

    Private Function CropBitmap(ByVal bmp As Bitmap, ByVal cropX As Integer, ByVal cropY As Integer, ByVal cropWidth As Integer, ByVal cropHeight As Integer) As Bitmap
        Dim rect As New Rectangle(cropX, cropY, cropWidth, cropHeight)
        Dim cropped As Bitmap = bmp.Clone(rect, Imaging.PixelFormat.Undefined) 'Exception points to this line
        Return cropped
    End Function

    Sub split(ByVal filename As String)
        Dim bmp As New Bitmap(filename)
        Dim fn = My.Computer.FileSystem.GetName(filename)
        Dim opdir = My.Computer.FileSystem.CurrentDirectory & "\images\" & fn
        dir(opdir) 'Just for creating a directory
        Dim w = bmp.Width, h = bmp.Height, wu As Integer = w / 5, hu As Integer = h / 5, x = 0, y = 0, count = 1
        For i As Integer = 1 To 5
            For j As Integer = 1 To 5
                Dim cbmp = CropBitmap(bmp, x, y, wu, hu)
                Dim dfn = "split_" & count & ".png"
                cbmp.Save(opdir & "\" & dfn, Imaging.ImageFormat.Png)
                x = x + wu
                count = count + 1
            Next
            x = 0
            y = y + hu
        Next
    End Sub

Thanks in advance...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.