fokz8 fokz8 - 15 days ago 6
ASP.NET (C#) Question

How to pass hex string color from .aspx to .ashx

How do i pass hex string value to my .ashx page, where i need it in System.Drawing.Color

If i pass a normal string like:

string name="John"


Everything works fine.

But if i try to pass hex value like:

string bg_color ="#000000"


the string on .ashx page is empty or " "

Already tried passing only values without #, which actually works. But if i add # later like:

Color _bgClr = ColorTranslator.FromHtml(string.Format{"#{0}", text_background_color);


I get error of # is invalid parameter when i try to change HEX(background values) on my webform.

Any clues?

Please help

UPDATE

<img class="link-realtime1" src="ImageGen.ashx?bo=<%=inp_barva_ozadja.Text %>&bt=<%=inp_barva_teksta.Text %>" style="float: left;margin-top: -20px;"/>


UPDATE2 - .aspx

<form id="image_generator" runat="server">
<div class="main">
<div class="controls">
<div class="options" style="float: left;width: 50%;">
<label>
<span>Širina</span>
<asp:TextBox ID="inp_sirina" Width="125" runat="server"></asp:TextBox>
</label><span style="color:red"><asp:Literal ID="napaka_s" runat="server"></asp:Literal></span><br />
<label>
<span>Višina</span>
<asp:TextBox ID="inp_visina" Width="125" runat="server"></asp:TextBox>
</label><span style="color:red"><asp:Literal ID="napaka_v" runat="server"></asp:Literal></span><br />
<label>
<span>Tekst</span>
<asp:TextBox ID="inp_text" Width="125" runat="server"></asp:TextBox>
</label><span style="color:red"><asp:Literal ID="napaka_t" runat="server"></asp:Literal></span><br />
<label>
<span>Velikost pisave</span>
<asp:TextBox ID="inp_velikost" Width="125px" runat="server"></asp:TextBox>
</label><span style="color:red"><asp:Literal ID="napaka_vp" runat="server"></asp:Literal></span><br />
<label>
<span>Barva ozadja</span>
<asp:TextBox ID="inp_barva_ozadja" Width="125px" runat="server"></asp:TextBox>
</label><span style="color:red"><asp:Literal ID="napaka_bo" runat="server"></asp:Literal></span><br />
<label>
<span>Barva teksta</span>
<asp:TextBox ID="inp_barva_teksta" Width="125px" runat="server"></asp:TextBox>
</label><span style="color:red"><asp:Literal ID="napaka_bt" runat="server"></asp:Literal></span><br />
<div id="colorpicker"></div><br />
<script type="text/javascript">
$(document).ready(function () {
$('#colorpicker').farbtastic('#inp_barva_ozadja');
});
</script>
<a class="link-realtime" href="ImageGen.ashx?w=<%= inp_sirina.Text %>&h=<%= inp_visina.Text %>&t=<%= inp_text.Text %>&ts=<%= inp_velikost.Text %>&bg=<%= inp_barva_ozadja.Text %>&tc=<%= inp_barva_teksta.Text %>">Generiraj sliko</a><br />
</div>
<!--<asp:Button ID="btn_generiraj" Width="125px" Text="Generiraj" runat="server" OnClick="btn_generiraj_Click" />
<asp:Button ID="btn_prenos" Width="125px" Text="Prenos" runat="server" />
<asp:Button ID="btn_pocisti" Width="125px" Text="Počisti" runat="server" /><br />
<asp:Image ID="imgText" runat="server" Visible="false" /></br>-->
<script type="text/javascript">
$('#inp_sirina, #inp_visina, #inp_barva_ozadja, #inp_barva_teksta, #inp_text, #inp_velikost').keyup(function () {
var tekst1 = $('#inp_sirina').val();
var tekst2 = $('#inp_visina').val();
var tekst3 = $('#inp_text').val();
var v_pisave = $('#inp_velikost').val();
var barva1 = $('#inp_barva_ozadja').val();
var barva2 = $('#inp_barva_teksta').val();;
var path = "ImageGen.ashx?w=" + tekst1 + "&h=" + tekst2 + "&t=" + tekst3 + "&ts=" + v_pisave + "&bg=" + barva1 + "&tc=" + barva2;
$("a.link-realtime").attr("href", path);
$("img.link-realtime1").attr("src", path);
});
</script>
<div id="output">
<span class="output-result"></span></br>
<img class="link-realtime1" src="ImageGen.ashx?w=<%= inp_sirina.Text %>&h=<%= inp_visina.Text %>&t=<%= inp_text.Text %>&ts=<%= inp_velikost.Text %>&bo=<%=inp_barva_ozadja.Text %>&bt=<%=inp_barva_teksta.Text %>" style="float: left;margin-top: -20px;"/>
</div></br>
</div>
</div>
</form>


UPDATE3 - .ashx

public void ProcessRequest(HttpContext context)
{

string _width = context.Request.QueryString["w"];
int __width = Convert.ToInt32(_width);
string _height = context.Request.QueryString["h"];
int __height = Convert.ToInt32(_height);
string text = context.Request.QueryString["t"];
string text_size = context.Request.QueryString["ts"];
int _text_size = Convert.ToInt32(text_size);
/*string text_background_color = context.Request.QueryString["bo"];*/
string text_background_color = "#000000";
Color _bgClr = ColorTranslator.FromHtml(text_background_color);
/*Color _bgClr = ColorTranslator.FromHtml(string.Format("{0}", "#" + text_background_color));*/
/*string text_color = context.Request.QueryString["bt"];*/
string text_color = "#FFFFFF";
Color _txtClr = ColorTranslator.FromHtml(text_color);

Bitmap slika = new Bitmap(1, 1);

float koordinataX = (__width / 2) - (((__width / 2) / 2) / 2);
float koordinataY = (__height / 2);

if (koordinataX < 50)
{
_text_size = _text_size / 2;
}

text = Regex.Replace(text, "(.{" + 20 + "})", "$1" + Environment.NewLine);

Font pisava = new Font("Verdana", _text_size, FontStyle.Bold, GraphicsUnit.Point);

Graphics graphics = Graphics.FromImage(slika);

slika = new Bitmap(slika, new Size(__width, __height));

graphics = Graphics.FromImage(slika);
graphics.Clear(_bgClr);
graphics.TextRenderingHint = TextRenderingHint.AntiAlias;

using (var sf = new StringFormat()
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center
})
{
graphics.DrawString(text, pisava, new SolidBrush(_txtClr), new Rectangle(0, 0, slika.Width, slika.Height), sf);
}

/*graphics.DrawString(text, pisava, new SolidBrush(_txtClr), koordinataX, koordinataY);*/

MemoryStream ms = new MemoryStream();
slika.Save(ms, ImageFormat.Png);

byte[] bajt = ms.ToArray();

context.Response.ContentType = "image/png";
context.Response.BinaryWrite(bajt);
context.Response.Flush();
}

Answer

Your string.Format code is not correct. Should be

string.Format("{0}", "#" + text_background_color)

But you can do without.

string text_background_color = "FF00FC";

Color _bgClr = ColorTranslator.FromHtml("#" + text_background_color);

UPDATE

You are converting the QueryString Parameters without checking for NULL values, so if one or more QueryString is not present or empty, your code will fail. It has nothing to do with setting the color.

string _width = context.Request.QueryString["w"];
string _height = context.Request.QueryString["h"];
string text = context.Request.QueryString["t"];
string text_size = context.Request.QueryString["ts"];
string text_background_color = context.Request.QueryString["bg"];
int __width = 300;
int __height = 200;      
int __text_size = 10;

if (!string.IsNullOrEmpty(_width))
{
    __width = Convert.ToInt32(_width);
}

if (!string.IsNullOrEmpty(_height))
{
    __height = Convert.ToInt32(_height);
}

if (!string.IsNullOrEmpty(text_size))
{
    __text_size = Convert.ToInt32(text_size);
}

if (!string.IsNullOrEmpty(text_background_color))
{
    text_background_color = "#" + text_background_color;
}
else
{
    text_background_color = "#000000";
}