Click here to Skip to main content
13,557,906 members
Rate this:
Please Sign up or sign in to vote.
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:

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 28-Mar-13 3:34am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

assuming your code is working - just change the scG.FillRectangles to (a looped) call to scg.DrawImage ...
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.180515.1 | Last Updated 28 Mar 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100