By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,682 Members | 1,407 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,682 IT Pros & Developers. It's quick & easy.

Bitmap drawing

P: n/a
Strange behaviour: I havea bitmap that I draw and add to an array of
bitmaps...
Do I have to create a bitmap using New every time?
I tried to draw the bitmap in a for loop alike this:
Dim mask As Image = getPicture("mask")
Dim tempMask As Bitmap=New Bitmap(tile.Width, tile.Height)
Dim grMask As Graphics= Graphics.FromImage(tempMask)
Dim x As Integer
For i As Integer = 0 To tile.Width * 14 Step tile.Width
grMask.DrawImage(mask, New Rectangle(0, 0, tile.Width,
tile.Height), New Rectangle(i, 0, tile.Width, tile.Height),
GraphicsUnit.Pixel)
imgTranzMask(x) = tempMask
x += 1
Next

When I try to draw each bitmap from array seems that I have the same image
in all elements... array elements is a reference to the same bitmap?
Nov 20 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Crirus" <Cr****@datagroup.ro> schrieb
Strange behaviour: I havea bitmap that I draw and add to an array
of bitmaps...
Do I have to create a bitmap using New every time?
Only whenever you want to create a new bitmap. :-)
I tried to draw the bitmap in a for loop alike this:
Dim mask As Image = getPicture("mask")
Dim tempMask As Bitmap=New Bitmap(tile.Width, tile.Height)
Dim grMask As Graphics= Graphics.FromImage(tempMask)
Dim x As Integer
For i As Integer = 0 To tile.Width * 14 Step tile.Width
grMask.DrawImage(mask, New Rectangle(0, 0, tile.Width,
tile.Height), New Rectangle(i, 0, tile.Width, tile.Height),
GraphicsUnit.Pixel)
imgTranzMask(x) = tempMask
x += 1
Next

When I try to draw each bitmap from array seems that I have the same
image in all elements... array elements is a reference to the same
bitmap?


Put creation of Bitmap within the loop, also call grMask.Dispose:

Dim mask As Image = getPicture("mask")
Dim x As Integer
For i As Integer = 0 To tile.Width * 14 Step tile.Width
Dim tempMask As Bitmap=New Bitmap(tile.Width, tile.Height)
Dim grMask As Graphics= Graphics.FromImage(tempMask)

grMask.DrawImage(mask, New Rectangle(0, 0, tile.Width,
tile.Height), New Rectangle(i, 0, tile.Width, tile.Height),
GraphicsUnit.Pixel)
grMask.Dispose
imgTranzMask(x) = tempMask
x += 1
Next

--
Armin

Nov 20 '05 #2

P: n/a
Hi Crirus,

You've only got one Bitmap. The Graphics, grMask, is dedicated to just
that bitmap and will always draw to it.
So, yes, you need to create a new BitMap each time, and also obtain the
Graphics to it.

Regards,
Fergus

ps Just as a small point, could I suggest swapping i and x?

Dim mask As Image = getPicture("mask")
Dim tempMask As Bitmap=New Bitmap(tile.Width, tile.Height)
Dim grMask As Graphics= Graphics.FromImage(tempMask)
Dim x As Integer

For i As Integer = 0 To 13
tempMask = New Bitmap(tile.Width, tile.Height)
grMask = Graphics.FromImage(tempMask)
grMask.DrawImage(mask, _
New Rectangle(0, 0, tile.Width, tile.Height), _
New Rectangle(x, 0, tile.Width, tile.Height), _
GraphicsUnit.Pixel)
imgTranzMask(i) = tempMask
x += tile.Width
Next
Nov 20 '05 #3

P: n/a
Yes, I know that way I will draw to the same bitmap, but my thought was...
.... I will draw the bitmap, put the result in the array, redraw the bitmap,
put the result in second element of array etc :)
"Fergus Cooney" <fi******@tesco.net> wrote in message
news:OP**************@tk2msftngp13.phx.gbl...
Hi Crirus,

You've only got one Bitmap. The Graphics, grMask, is dedicated to just
that bitmap and will always draw to it.
So, yes, you need to create a new BitMap each time, and also obtain the Graphics to it.

Regards,
Fergus

ps Just as a small point, could I suggest swapping i and x?

Dim mask As Image = getPicture("mask")
Dim tempMask As Bitmap=New Bitmap(tile.Width, tile.Height)
Dim grMask As Graphics= Graphics.FromImage(tempMask)
Dim x As Integer

For i As Integer = 0 To 13
tempMask = New Bitmap(tile.Width, tile.Height)
grMask = Graphics.FromImage(tempMask)
grMask.DrawImage(mask, _
New Rectangle(0, 0, tile.Width, tile.Height), _
New Rectangle(x, 0, tile.Width, tile.Height), _
GraphicsUnit.Pixel)
imgTranzMask(i) = tempMask
x += tile.Width
Next

Nov 20 '05 #4

P: n/a
Hi Crirus,

|| ... I would draw the bitmap, put the result in the array,
|| redraw the bitmap, put the result in second element
|| of array etc :)

That would work well if the array took a copy each time but it only holds
the reference, and so each array item gets the same reference.

Regards,
Fergus
Nov 20 '05 #5

P: n/a
Yes, I know now, but the first image resulted was a shock :)

"Fergus Cooney" <fi******@tesco.net> wrote in message
news:%2******************@TK2MSFTNGP12.phx.gbl...
Hi Crirus,

|| ... I would draw the bitmap, put the result in the array,
|| redraw the bitmap, put the result in second element
|| of array etc :)

That would work well if the array took a copy each time but it only holds the reference, and so each array item gets the same reference.

Regards,
Fergus

Nov 20 '05 #6

P: n/a
"Crirus" <Cr****@datagroup.ro> schrieb
Yes, I know that way I will draw to the same bitmap, but my thought
was... ... I will draw the bitmap, put the result in the array,
redraw the bitmap, put the result in second element of array etc
:)


Bitmaps are reference types, so "imgTranzMask(i) = tempMask" doesn't copy
the bitmap, it only copies the reference. As you only create one Bitmap, all
items in the array point to the same bitmap.
--
Armin

Nov 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.