使用C#单击按钮从文本生成图像 [英] Generate images from text on clicking a button using C#

查看:96
本文介绍了使用C#单击按钮从文本生成图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在按钮点击时随机生成图像使用c#

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;

}





我的尝试:



i无法找到任何人可以帮助我,因为我是新手



What I have tried:

i could not find any source can anyone help me out as i am new to this

推荐答案

我们不做你的功课:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是和你想的一样困难!



如果遇到具体问题,请询问相关问题,我们会尽力提供帮助。但是我们不会为你做这一切!



但我会给你一些指示:

1)看看随机类: [ ^ ] - 它有一个GetNext方法,它对返回值设置上限和下限,允许您选择五位数字。

2)从用户那里获取计数,并查看使用 int.TryPasre [ ^ ]将其转换为整数值。 (或者更好的是,使用NumericUpDown控件而不是TextBox并直接获取验证值。

3)使用结果为(2)的循环重复调用(1)。



为了防止重复,我会填写一个List< int>使用所有有效的五位数字,然后使用Random类选择一个索引。选择后,从列表中删除值(这将减少最大索引)
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)


我们不做你的HomeWork。

HomeWork未设置为测试你的乞求其他人做你的工作的技巧,它会让你思考并帮助你的老师检查你对所学课程的理解,以及你应用它们时遇到的问题。

你的任何失败都会帮助你的老师发现你的弱点并设定补救措施。

所以,试一试,重读你的课程并开始工作。如果您遇到特定问题,请显示您的代码并解释这个问题,我们可能会提供帮助。

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



作为程序员,你的工作是创建算法来解决具体问题问题,你不能依赖别人永远为你做,所以有一段时间你将不得不学习如何。而且越快越好。

当你要求解决方案时,就像试图通过培训其他人来学习开车一样。

创建算法基本上是找到数学并做出必要的调整以适应你的实际问题。



拿一张纸,用2个数字模拟你的问题。注意如何继续执行程序将遵循相同的步骤。


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.


这篇关于使用C#单击按钮从文本生成图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆