Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# C#4.0 graphics image Bitmap , +
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 Smile | :)
Posted 28-Mar-13 4:34am

1 solution

Rate this: bad
good
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 ...
  Permalink  
Comments
kastriotlimani at 28-Mar-13 10:33am
   
i still cant make it work... can u show me the lines of code please
johannesnestler at 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 at 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 at 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
0 OriginalGriff 274
1 Shweta N Mishra 216
2 PIEBALDconsult 210
3 Sergey Alexandrovich Kryukov 185
4 BillWoodruff 174
0 OriginalGriff 7,630
1 Sergey Alexandrovich Kryukov 7,022
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,525


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 28 Mar 2013
Copyright © CodeProject, 1999-2014
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