0
我要創建「應用程序」,將來自用戶的輸入繪製圖像 - 文本值如何從文本框繪製圖像值
我使用System.Drawing中這樣做,但是當我運行的應用程序,螢火蟲控制檯回報沒有元素中找到
用戶輸入例如
寬度:600
身高:400
文字:你好世界
背景色:#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);
}
我到底做錯了什麼?
謝謝!
給我舉個例子嗎? – aiden87
字符串文本,int寬度,int高度,顏色bg_color,顏色txt_color,float font_size是全局變量或添加包含GenerateImg(Grapics g,字符串文本,int寬度,int高度,顏色bg_color,顏色txt_color,float font_size)函數 –