473,387 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Bitmap drawing

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
6 1799
"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
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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: active | last post by:
I find Bitmap.Save works for WMF files but Bitmap.FromFile does not. If I use FromFile on a WMF file that came with VS I get an exception. If I use it on a WMF file created with Bitmap.Save I...
2
by: Peter Proost | last post by:
Hi group I have the following piece of code that should resize the bitmap in a picture box but it doesn't work as I tought it would. Can someone help me with it? thnx Peter Public Class...
8
by: Nathan Sokalski | last post by:
I am trying to write code to rotate a graphic that I have. Here is the code I am currently using: Dim frogbitmap As New Bitmap(Drawing.Image.FromFile(Server.MapPath("images/frog.gif"))) Dim...
7
by: Fir5tSight | last post by:
Hi All, I used the following code in C#: using System.Drawing; //blah blah blah Bitmap bmp = new Bitmap();
15
by: Hamed | last post by:
Have I posted the message to wrong newsgroup? Or Does the question is so much strage? Would someone please kindly direct me to a true newsgroup or resource? Best Regards Hamed
12
by: Lee | last post by:
I have the following code using System; using System.Windows.Forms; using System.Drawing; using System.Collections; namespace FINN {
7
by: Earl | last post by:
I have an image issue that I do not understand. When I try to save a bitmap created from a picturebox image, I can save without exception so long as the bitmap was retrieved from a file and loaded...
0
by: benfly08 | last post by:
Hi, guys. I have a program to draw bar/pie chart based on the data i hard coded in it. However, my image comes with "BLACK" background color. I don't know how to fix this. The code snippet is...
8
by: Joergen Bech | last post by:
Suppose I have Dim bm As New Bitmap(16, 16,Imaging.PixelFormat.Format8bppIndexed) I cannot use Dim g As Graphics = Graphics.FromImage(bmdest) Dim hdc As IntPtr = g.GetHdc() as the...
1
by: martinsmith160 | last post by:
Hi all I am trying to create a level builder tool for a final year project and im having some problems drawing. I have placed a picture box within a panel so i can scroll around the image which is...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.