Bitmap drawing

C

Crirus

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?
 
A

Armin Zingler

Crirus said:
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
 
F

Fergus Cooney

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
 
C

Crirus

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 :)
 
F

Fergus Cooney

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
 
A

Armin Zingler

Crirus said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top