Images in array with for/next problems
Images in array with for/next problems
Images in array with for/next problems
Images in array with for/next problems
Images in array with for/next problems
Images in array with for/next problems Images in array with for/next problems Images in array with for/next problems Images in array with for/next problems Images in array with for/next problems Images in array with for/next problems Images in array with for/next problems Images in array with for/next problems
Images in array with for/next problems Images in array with for/next problems
Images in array with for/next problems
Go Back  Xtreme Visual Basic Talk > > > Images in array with for/next problems


Reply
 
Thread Tools Display Modes
  #1  
Old 08-21-2012, 09:02 PM
Under Study Under Study is offline
Regular
 
Join Date: Jul 2008
Posts: 83
Default Images in array with for/next problems


Hey guys,
Im trying to take a rather large image from my resources and using a for/next loop break it into smaller images that i save in a bitmap array. Here is what i have so far.

Code:

    Public Areas(1000, 1000) As Image
    Public bm As New Bitmap(100, 100)
    Public xx As Integer
    Public yy As Integer
    Public a As Integer = 1
    Public b As Integer
    Public newmap As Bitmap = My.Resources.Lops
    Public Mapps As Bitmap = My.Resources.map1
    Public inttop As Integer = 0
    Public intleft As Integer = 0
    Public intwidth As Integer = 100
    Public intheight As Integer = 100
    Public land As Bitmap

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For xx = 1 To 10

            Using gr As Graphics = Graphics.FromImage(bm)
            ' Define source and destination rectangles.
                 Dim src_rect As New Rectangle(intleft, inttop, 100, 100)
                 Dim dst_rect As New Rectangle(0, 0, 100, 100)

            ' Copy that part of the image.
                 gr.DrawImage(Mapps, dst_rect, src_rect, GraphicsUnit.Pixel)

            Areas(a, 1) = bm
            intleft = intleft + 10
            a = a + 1
        Next
        PictureBox1.Image = Areas(1, 1)
        PictureBox2.Image = Areas(2, 1)
        PictureBox3.Image = Areas(3, 1)
        PictureBox4.Image = Areas(4, 1)
        PictureBox5.Image = Areas(5, 1)
        PictureBox6.Image = Areas(6, 1)
        PictureBox7.Image = Areas(7, 1)
        PictureBox8.Image = Areas(8, 1)
        PictureBox9.Image = Areas(9, 1)
        PictureBox10.Image = Areas(10, 1)


    End Sub


what that should do, in my head, is after setting the array "areas" do 10 different images, in 10 different places in the areas array then put each one in a different imgbox, only problem is that all 10 images are coming up the same. when i click the button again i get a new image 10pix to the right in all 10 pictureboxes.

any ideas on why?
im in vb08

thanks in advance.
Reply With Quote
  #2  
Old 08-21-2012, 10:20 PM
passel's Avatar
passelImages in array with for/next problems passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,024
Default

bm is a reference type and "points" to a bitmap.
When you assign bm to the elements of your array, they will all point to the same bitmap.
That bitmap will contain the last thing drawn in it.
You can either set each element of the array to bm.Clone, so that you will get a new copy of bm at the time it is cloned (easiest to do with the existing code).
Areas(a, 1) = bm.Clone

Or you could change the logic a bit, and not use bm, but create a new bitmap in your array, get a graphics object for it and draw on it directly.
Code:
        ' Define source and destination rectangles.
        Dim src_rect As New Rectangle(intleft, inttop, 100, 100)
        Dim dst_rect As New Rectangle(0, 0, 100, 100)
        For xx = 1 To 10
            Areas(a,1) = new Bitmap(100,100)
            Using gr As Graphics = Graphics.FromImage(Areas(a,1))

            ' Copy that part of the image.
                 gr.DrawImage(Mapps, dst_rect, src_rect, GraphicsUnit.Pixel)
            End Using
            intleft = intleft + 10
            src_rect.X = intleft
            a = a + 1
        Next
Don't know which would be faster or better on the resources.
I guess, for simplicity at this point, just assign a bm.clone.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.

Last edited by passel; 08-21-2012 at 10:36 PM.
Reply With Quote
  #3  
Old 08-22-2012, 03:06 PM
Under Study Under Study is offline
Regular
 
Join Date: Jul 2008
Posts: 83
Default

Awesome, I did a little recode like you showed and it works... now im just not sure why it works... I like to make sure i know exactly what my code is doing so i can improve apron it later. What exactly is function or purpose of the
" src_rect.X = intleft " part of the code? I am generally not experienced with the drawimage function.

Thanks.
Reply With Quote
  #4  
Old 08-22-2012, 04:09 PM
passel's Avatar
passelImages in array with for/next problems passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,024
Default

It was just an efficiency.
You were re-declaring new rectangles each time through the loop, but you really were only changing the X value of the location (the first parameter in your new rectangle call), in the source one.
So, rather than create a new rectangle repeatedly, I just create it once before the loop, and modify the X value.
Later I assume you'll also by modifying the Y property to position the rectangle vertically.
The destination one doesn't need to be modified, so just creating it once makes sense.

There are other shorter ways of doing some things, such as
intleft += 10 'is the same as intleft = intleft + 10
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #5  
Old 08-22-2012, 04:41 PM
Under Study Under Study is offline
Regular
 
Join Date: Jul 2008
Posts: 83
Default

Are there any good tutorials that you would recommend on the drawimage function that could help me understand it better?
Reply With Quote
  #6  
Old 08-22-2012, 08:06 PM
passel's Avatar
passelImages in array with for/next problems passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,024
Default

I just decided to sit down and finally learn how drawing was done in VB.Net over Christmas Break a years and a half ago (did all the old legacy VB and earlier versions of various BASICs for many years).

I simply searched the internet for the three words "VB.Net" "GDI+" and "tutorial".
Reading through a number of them, trying things out, and develop a sense of right and wrong by reading a wide variety of sources has helped me on my way considerably.
The articles by Bob Powell are vary similiar to the things I've determined through my trials, so I think they are a reasonably good source of accurate information.
Other than that, I don't any particular tutorials in mine.
Interestingly enough, some of the examples given in Microsoft's MSDN documentation don't seem to follow good practices, so I do like to search for multiple examples to compare and try to determine a reasonably good approach to using a capability.
But these languages and libraries just keep growing bigger (and I older) so I don't think I'll ever have the feeling of knowing a programming language "like the back of my hand" again.
Anyway, good luck with your journey.
figured out and are along the lines of what I might write, if I was so inclined, so would probably write from
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Images in array with for/next problems
Images in array with for/next problems
Images in array with for/next problems Images in array with for/next problems
Images in array with for/next problems
Images in array with for/next problems
Images in array with for/next problems Images in array with for/next problems Images in array with for/next problems Images in array with for/next problems Images in array with for/next problems Images in array with for/next problems Images in array with for/next problems
Images in array with for/next problems
Images in array with for/next problems
 
Images in array with for/next problems
Images in array with for/next problems
 
-->