Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, i am a beginner at coding, and i am making a little game where a character is capturing some dots generated randomly.
Now i need each dot to be an image (same image for all), but i cant find out how to do it.
This is a piece of what I did:

C#
Rectangle[] rects;
rects = new Rectangle[10];
Bitmap background;
Graphics scG;

public Rectangle[] makerect()
        {
            Random r = new Random();

            for (int i = 0; i < rects.Length; i++)
            {
                rects[i] = new Rectangle(r.Next(0,this.Width),r.Next(0,this.Height),10,10);
            }
            return rects;
        }

 public Bitmap Draw()
        {
            
            scG.FillRectangles(Brushes.Gold, rects);
            return background;

        }


If anyone can help me with some lines of code, I'd appreciate it :)
Posted

1 solution

assuming your code is working - just change the scG.FillRectangles to (a looped) call to scg.DrawImage ...
 
Share this answer
 
Comments
kastriotlimani 28-Mar-13 10:33am    
i still cant make it work... can u show me the lines of code please
johannesnestler 28-Mar-13 10:46am    
what is your exact problem? - I like to show you the lines to change, but you didn't provide a runnable solution... I just assume your FillRectangles call now is ok - yes? So Just draw the Image instead of filling the rectangles with a solid color brush.
So you could write something like this:

Bitmap bmp = new Bitmap("C:\ValidDirectory\ValidImageFilePath.jpg");
foreach(Rectangle rect in rects)
scG.DrawImage(bmp, rect)

(in real solution just load the image once - not during every Draw()-call)
johannesnestler 28-Mar-13 11:00am    
thank you - now it's easier to see if you have another problem - so I was right - just do as I said in my last comment and it should draw Images instead of your golden rects (you don't have to load the Bitmap from file, I'd add a Resource to the project and use that)
kastriotlimani 28-Mar-13 11:36am    
Thank You, it worked finally :)
Have a nice day !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900