如何从文本框值中绘制图像

时间:2016-11-20 10:27:46

标签: c# asp.net image bitmap system.drawing

我想创建" app"这将从用户输入中绘制图像 - 文本框值

我是使用System.Drawing做的,但是当我运行app时,firebug控制台返回找不到元素

用户输入示例

宽度:600

身高:400

文字:Hello world

背景色:#000000

文字颜色:#ffffff

结果 - 600x400图片,#000背景颜色和文字" hello world"有色#fff

generateImg.aspx

div class="main">
    <div class="controls">
        <div class="options">
            <label>
                <span>Width</span>
                <asp:TextBox ID="inp_width" Width="125" Text="600" runat="server"></asp:TextBox>
            </label><span style="color:red"><asp:Literal ID="error1" runat="server"></asp:Literal></span><br />
            <label>
                <span>Height</span>
                <asp:TextBox ID="inp_height" Width="125" Text="400" runat="server"></asp:TextBox>
            </label><span style="color:red"><asp:Literal ID="error2" runat="server"></asp:Literal></span><br />
            <label>
                <span>Text</span>
                <asp:TextBox ID="inp_text" Width="125" Text="Poljuben tekst" runat="server"></asp:TextBox>
            </label><span style="color:red"><asp:Literal ID="error3" runat="server"></asp:Literal></span><br />      
            <label>
                <span>Font size</span>
                <asp:TextBox ID="inp_font_size" Width="125px" Text="48" runat="server"></asp:TextBox>
            </label><span style="color:red"><asp:Literal ID="error4" runat="server"></asp:Literal></span><br />           
            <label>
                <span>Background color</span>
                <asp:TextBox ID="inp_bg_color" Width="125px" Text="#000000" runat="server"></asp:TextBox>
            </label><span style="color:red"><asp:Literal ID="error5" runat="server"></asp:Literal></span><br />
           <label>
                <span>Text color</span>
                <asp:TextBox ID="inp_text_color" Width="125px" Text="#ffffff" runat="server"></asp:TextBox>
            </label><span style="color:red"><asp:Literal ID="error6" runat="server"></asp:Literal></span><br />
            <div id="colorpicker"></div><br />
            <script type="text/javascript">
                $(document).ready(function() {
                    $('#colorpicker').farbtastic('#inp_bg_color');
                });
           </script>                        
        </div>
        <asp:Button ID="btn_generate" Width="125px" Text="Generate" runat="server" OnClick="btn_generate_Click" />
        <asp:Button ID="btn_download" Width="125px" Text="Download" runat="server" />
        <asp:Button ID="btn_clear" Width="125px" Text="Clear" runat="server" />
    </div>
</div>

generateImg.aspx.cs

private static Bitmap GenerateImg(string text, int width, int height, Color   bg_color, Color txt_color, float font_size)
    {
        Bitmap bmpImg = new Bitmap(1, 1);

        int _width = width;
        int _height = height;

        float cordX = (width / 2) - ((width / 2) / 2) / 2;
        float cordY = (heigh / 2) - ((heigh / 2) / 2) / 2;

        Font font = new Font("Verdana", font_size, FontStyle.Bold, GraphicsUnit.Point);

        Graphics graphics = Graphics.FromImage(bmpImg);

        bmpImg = new Bitmap(bmpImg, new Size(_width, _height));

        graphics = Graphics.FromImage(bmpImg);
        graphics.Clear(bg_color
        graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
        graphics.DrawString(text, font, new SolidBrush(txt_color), cordX, cordY);

        graphics.Flush();
        return (bmpImg);
    }

protected void btn_generate_Click(object sender, EventArgs e)
    {


        int width = Convert.ToInt32(inp_width.Text);
        int height = Convert.ToInt32(inp_height.Text);
        int font_size = Convert.ToInt32(inp_font_size.Text);
        string text = inp_text.Text;
        string hex1 = inp_bg_color.Text;
        Color bgClr =ColorTranslator.FromHtml(hex1);
        string hex2 = inp_txt_color.Text;
        Color txtClr = ColorTranslator.FromHtml(hex2);

        Bitmap bmp = GenerateImg(text, width, height, bgClr, txtClr, font_size);

            }

我做错了什么?

谢谢你!

1 个答案:

答案 0 :(得分:0)

如何覆盖onPaint()?

class Class1 : TextBox
{
    ...

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        GenerateImg(e.Graphics);
    }

private static void GenerateImg(Graphics g, string text, Color bg_color, Color txt_color, float font_size)
    {
        int _width = width;
        int _height = height;

        float cordX = (width / 2) - ((width / 2) / 2) / 2;
        float cordY = (heigh / 2) - ((heigh / 2) / 2) / 2;

        Font font = new Font("Verdana", font_size, FontStyle.Bold, GraphicsUnit.Point);

        Graphics graphics = g;
        graphics.Clear(bg_color);
        graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
        graphics.DrawString(text, font, new SolidBrush(txt_color), cordX, cordY);
        graphics.Flush();
}
...
}