Blog

Split multipage image into single page image in VB.Net

Posted by Amit Patel Blogs | Visual Basic .NET Oct 17, 2012
This code convert your multipage image into single image with new name.
Download Files: SplitImage.rar
This code convert your multipage image into single image with new name.

First you Imports System.Drawing.Imaging in your project. Then on a button click event handler, write the following code. 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'SplitImage(Image Path,Destination Path)
    SplitImage("D:\Fax\rraannaammeett.G3N", "D:\Amit\converted\")
End Sub

Private Function SplitImage(ByVal SourcePath As String, ByVal DestPath As String)
        
        'Dim FileSubString As String = System.IO.Path.GetFileNameWithoutExtension(files)

        'this code for get file/image name from path
        Dim Dir = SourcePath
        Dim path() As String = Dir.Split("\")
        Dim chunk As Integer = path.Length
        Dim ImageName As String = path(chunk - 1)
        ImageName = ImageName.Replace(".G3N", "")

        Dim pagecount, ImagePageCount As Integer
        Dim oimage As System.Drawing.Image = Nothing
        Dim fImage As Bitmap = Nothing
        Dim pageLoad As Boolean = False
        Dim docPath As String = DestPath
        Try
            fImage = New Bitmap(SourcePath, False)
            oimage = fImage
            pagecount = oimage.GetFrameCount(FrameDimension.Page)
            pageLoad = True
        Catch ex As Exception
            pageLoad = False
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        If pageLoad = True Then
            If pagecount <> 0 Then
                For ImagePageCount = 0 To pagecount - 1
                    Dim strScanKey As String = System.Guid.NewGuid.ToString
                    oimage.SelectActiveFrame(FrameDimension.Page, ImagePageCount)
                    Dim img1 As Drawing.Bitmap = CType(oimage.Clone, Drawing.Bitmap)
                    Dim documentPath As String = docPath & ImageName & "_" & ImagePageCount + 1 & ".jpg"
                    img1.Save(documentPath)
                Next
            End If
            oimage.Dispose()
            fImage.Dispose()
        End If
End Function

PREMIUM SPONSORS
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.
SPONSORED BY
  • MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.