【问题标题】:How to pass hex string color from .aspx to .ashx如何将十六进制字符串颜色从 .aspx 传递到 .ashx
【发布时间】:2017-04-05 20:56:25
【问题描述】:

如何将十六进制字符串值传递给我的 .ashx 页面,我在 System.Drawing.Color 中需要它

如果我传递一个普通的字符串,例如:

string name="John"

一切正常。

但如果我尝试传递十六进制值,例如:

string bg_color ="#000000"

.ashx 页面上的字符串为空或“”

已经尝试只传递不带# 的值,这确实有效。但是,如果我稍后添加 # 之类的:

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

当我尝试在我的网络表单上更改 HEX(背景值)时,我收到 # is invalid parameter 错误。

有什么线索吗?

请帮忙

更新

<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>
        <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();
    }

【问题讨论】:

  • 您是否尝试过将参数bg_color 传递给.ashx 的url 编码?
  • 如果 url 编码与我所做的不同(请参阅更新),请您在我的表单上发布示例@BobDust

标签: c# asp.net hex ashx


【解决方案1】:

您的string.Format 代码不正确。应该是

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

但你可以不用。

string text_background_color = "FF00FC";

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

更新

您正在转换 QueryString 参数而不检查 NULL 值,因此如果一个或多个 QueryString 不存在或为空,您的代码将失败。它与设置颜色无关。

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";
}

【讨论】:

  • 奇怪,你们似乎都这么认为。是的,虽然它可能应该工作,但我收到此错误 = 附加信息:# 不是 Int32 的有效值。当页面加载并具有默认值 000000 时不会发生这种情况,仅当我尝试手动将其更改为 ffffff 或任何其他时才会发生
  • Int32 错误?您在哪里以及如何使用_bgClrchange it manually 是什么意思?问题可能出在您代码中的其他地方。
  • 我有一个 Web 表单,它有 6 个不同的文本框(宽度、高度、文本、字体大小、背景颜色和文本颜色)...整个表单在代码后面都有默认值...现在用户可以输入他自己的值并生成所需的图像...我得到所有用户值(宽度、高度、文本、字体大小),除了背景颜色和文本颜色...这意味着图像按照用户的需要创建,但没有t 改变颜色
  • 您将不得不发布更多代码,使用当前的 sn-p 这是不可能解决的。 ashx 文件中的代码是什么样的?
  • 刚刚更新了所有代码。一些代码被注释为我在尝试不同的东西(特别是在 .ashx 中)
【解决方案2】:

我认为参数bg_color的值的简单url编码如下:

<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 %>&bg_color=<%=HttpUtility.UrlEncode("#000000") %>" style="float: left;margin-top: -20px;"/>

可以在通用处理程序中检索如下:

var bgColor = context.Request.QueryString["bg_color"];

【讨论】:

  • 但这仅适用于#000000的预分配值,不是吗?意味着在线表格上,用户将无法将其更改为他想要的任何内容。还是我错了?
  • 是的,这个答案侧重于传递参数。对于传递用户输入的值,您需要从颜色选择器inp_barva_ozadja 中读取这些值。
猜你喜欢
  • 2014-04-25
  • 2012-02-16
  • 2011-07-12
  • 1970-01-01
  • 1970-01-01
  • 2012-11-01
  • 2012-11-24
  • 2018-07-24
  • 1970-01-01
相关资源
最近更新 更多