Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I need to generate images randomly on button click using c#
private Image DrawText(String text, Font font, Color textColor, Color backColor)
{
    //first, create a dummy bitmap just to get a graphics object
    Image img = new Bitmap(1, 1);
    Graphics drawing = Graphics.FromImage(img);

    //measure the string to see how big the image needs to be
    SizeF textSize = drawing.MeasureString(text, font);

    //free up the dummy image and old graphics object
    img.Dispose();
    drawing.Dispose();

    //create a new image of the right size
    img = new Bitmap((int) textSize.Width, (int)textSize.Height);

    drawing = Graphics.FromImage(img);

    //paint the background
    drawing.Clear(backColor);

    //create a brush for the text
    Brush textBrush = new SolidBrush(textColor);

    drawing.DrawString(text, font, textBrush, 0, 0);

    drawing.Save();

    textBrush.Dispose();
    drawing.Dispose();

    return img;

}


What I have tried:

i could not find any source can anyone help me out as i am new to this
Posted
Updated 24-Jan-17 16:48pm
v3

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!

But I'll give you a few pointers:
1) Look at the Random Class: [^] - it has a GetNext method which takes an upper and lower limit on the return value which allows you to select five digit numbers.
2) Get the count from the user, and look at using int.TryPasre[^] to convert it to an integer value. (Or better, use a NumericUpDown control instead of a TextBox and get the validated value directly.
3) Use a loop with the result of (2) to call (1) repeatedly.

To prevent repeats, I'd fill a List<int> with all the valid five digit numbers, and then use the Random class to select an index into it. Once selected, remove the value from the List (which will reduce the maximum index)
 
Share this answer
 
Comments
OriginalGriff 24-Jan-17 10:42am    
I provided you links that give you sufficient information to work it out: you will learn nothing from a "full solution" (if one existed for each homework question, which it doesn't) and it would be unfair on your classmates.
Give it a try, it's pretty simple and should take you less time than you have already spent trying to avoid doing it yourself! :laugh:
OriginalGriff 24-Jan-17 10:43am    
Oh, and by the way...
You do realize that your tutor probably checks all of these sites and will know immediately if you copied a solution from them? Write your own using the info I gave you...
OriginalGriff 24-Jan-17 11:00am    
Why? They are part of the conversation!
We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.
Quote:
i could not find any source can anyone help me out as i am new to this

As programmer, your job is to create algorithms that solve specific problems and you can't rely on someone else to eternally do it for you, so there is a time where you will have to learn how to. And the sooner, the better.
When you just ask for the solution, it is like trying to learn to drive a car by having someone else training.
Creating an algorithm is basically finding the maths and make necessary adaptation to fit your actual problem.

Take a sheet of paper and simulate your problem with 2 figures numbers. Note how you proceed the program will follow the same procedure.
 
Share this answer
 

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