Results 1 to 7 of 7

Thread: split image

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    118

    split image

    Hello,

    I want to make a picture splitter, this means when i have a picture from 64x64 it should split is to 4 images of 32x32

    how will I code this?

    Thanks,

    Jimpie

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: split image

    You create a new Bitmap of the appropriate size, call Graphics.FromImage to create a Graphics object, then call Graphics.DrawImage to draw the appropriate part of the original Image onto the new Image. Do that four times and you've got your wish. You should read the documentation for Graphics.DrawImage to find the appropriate overload to call.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    118

    Re: split image

    Quote Originally Posted by jmcilhinney View Post
    You create a new Bitmap of the appropriate size, call Graphics.FromImage to create a Graphics object, then call Graphics.DrawImage to draw the appropriate part of the original Image onto the new Image. Do that four times and you've got your wish. You should read the documentation for Graphics.DrawImage to find the appropriate overload to call.
    Hello,

    I can't get it working, i've never worked with splitting bitmaps before. I tried diffrent things but I can't. Could you help a bit more?

    Code:
        Private Sub btnSplit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSplit.Click
    
            Dim G As Graphics = Me.PictureBox2.CreateGraphics
            Dim tileset As Bitmap
            Dim newtileset As Bitmap
            Dim iRect As Rectangle
            Dim sRect As Rectangle
    
            tileset = New Bitmap(PictureBox.Image)
    
            For x = 0 To (tileset.Width / 32)
                For y = 0 To (tileset.Height / 32)
    
                    iRect = New Rectangle(x * 32, y * 32, 32, 32)
                    sRect = New Rectangle(x * 32, y * 32, 32, 32)
                    G.DrawImage(tileset, iRect, sRect, GraphicsUnit.Pixel)
    
                    G = Graphics.FromImage(tileset)
    
                    newtileset = tileset
    
                Next
            Next
    
    
        End Sub

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: split image

    Quote Originally Posted by jmcilhinney
    call Graphics.FromImage to create a Graphics object
    Quote Originally Posted by jimpie
    Code:
    Dim G As Graphics = Me.PictureBox2.CreateGraphics
    Do you see a discrepancy there?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    118

    Re: split image

    Quote Originally Posted by jmcilhinney View Post
    Do you see a discrepancy there?
    I can't get it work, its now giving just a transparent png file with size 32x32

    Code:
        Private Sub btnSplit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSplit.Click
    
            Dim png As Bitmap
            Dim g As Graphics
            Dim iRect As Rectangle
            Dim sRect As Rectangle
    
            g = PictureBox2.CreateGraphics
    
            'our new file
            png = New Bitmap(32, 32)
    
            'create graphic object
            iRect = New Rectangle(0, 0, 32, 32)
            sRect = New Rectangle(32, 32, 32, 32)
            g.DrawImage(PictureBox.Image, iRect, sRect, GraphicsUnit.Pixel)
    
            g = Graphics.FromImage(png)
    
            png.Save("test.png", ImageFormat.Png)
    
        End Sub

  6. #6
    Hyperactive Member Ram2Curious's Avatar
    Join Date
    Apr 2010
    Posts
    484

    Re: split image

    Take a look at this example.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    118

    Smile Re: split image

    Quote Originally Posted by Ram2Curious View Post
    Take a look at this example.
    thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width