user2183919
user2183919

Reputation: 73

c# drawing objects at once instead of using a loop

Hello i am trying to draw about 1000 images and between 10 to 100 rectangels and elipses. But i need all of them to show up on screen only when they done loading(not in a loading screen but in a game or slideshow). so for example

texturegrass = MyApp.Properties.Resources.Grass
Rectangle[] rects;
recs = new Rectangle[1000]
for (int i = 0; i < rects.Length; i++)
        {
            g.DrawImage(texturegrass,rects[i]);
        }

this is what i done so far but every rectangle is been drawn by it own what cause a flickering problam.

I have double bufferd the app. I tried using parallel but the application keep crashing

I hope one of you guys can help me...


##*

Upvotes: 2

Views: 1680

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726929

You can use a Graphics object to create the image off-screen, and then draw the image on the screen using the screen's Graphics object.

var bmp = new Bitmap(MyWidth, CMyHeight);
var gOff = Graphics.FromImage(bmp);
gOff.FillRectangle(new SolidBrush(Color.White), 0, 0, bmp.Width, bmp.Height);
texturegrass = MyApp.Properties.Resources.Grass
Rectangle[] rects = ...;
recs = new Rectangle[1000]
for (int i = 0; i < rects.Length; i++) {
    gOff.DrawImage(texturegrass,rects[i]);
}

At this point you can draw bmp all at once on the screen's Graphic.

Microsoft: How to Draw Images Off-Screen

Upvotes: 3

Related Questions