2016-11-20 1 views
0

textbox Je veux créer « app » qui attirerait l'image des utilisateurs entrée - valeurs textboxComment dessiner l'image de valeurs

Je le fais en utilisant System.Drawing, mais quand je lance l'application, le retour de la console Firebug aucun élément trouvé

exemple d'entrée d'utilisateur

Largeur: 600

Taille: 400

Texte: Bonjour tout le monde

background-color: # 000000

Text-color: # ffffff

Résultat - image 600x400 aveC# 000 couleur et texte de fond "bonjour monde" couleur #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); 

      } 

ce que je fais mal?

merci!

Répondre

0

Comment remplacer une 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(); 
} 
... 
} 
+0

montrez-moi un exemple? – aiden87

+0

texte de la ligne, largeur int, hauteur int, couleur bg_color, couleur txt_color, float taille_fonte est la variable globale ou ajouter la fonction include GenerateImg (Grapics g, texte de la chaîne, largeur int, hauteur int, couleur bg_color, couleur txt_color, float font_size) –