c# - 在 Windows Phone 7 应用程序的 c# 中将字符串转换为图像

标签 c# windows-phone-7.1

我想将用户输入的字符串转换为图像..该怎么做? 我尝试了以下代码,但在该行中出现了参数异常: WriteableBitmap wbimg = PictureDecoder.DecodeJpeg(memStream);

    static public string EncodeTo64(string toEncode)
    {
        byte[] toEncodeAsBytes
              = StringToAscii(toEncode);
        string returnValue
              = System.Convert.ToBase64String(toEncodeAsBytes);
        return returnValue;
    }

    public static byte[] StringToAscii(string s)
    {
        byte[] retval = new byte[s.Length];
        for (int ix = 0; ix < s.Length; ++ix)
        {
            char ch = s[ix];


            if (ch <= 0x7f)  retval[ix] = (byte)ch; 
            else  retval[ix] = (byte)'?'; 
        }

        return retval;
    }
    void convert()
    {
        String s = textBox1.Text;
        byte[] data = Convert.FromBase64String(EncodeTo64(s));

        for (int i = 0; i < data.Length; i++)
        {
            System.Diagnostics.Debug.WriteLine(data[i]);
        }
        Stream memStream = new MemoryStream();
        memStream.Write(data, 0, data.Length);


        try
        {
        WriteableBitmap wbimg = PictureDecoder.DecodeJpeg(memStream);

        image1.Source = wbimg;
        }
        catch (Exception e)
        {
            MessageBox.Show(e.ToString());

        }

    }

我在下面的链接中得到了我想要的东西.. How can I render text on a WriteableBitmap on a background thread, in Windows Phone 7?http://blogs.u2u.be/michael/post/2011/04/20/Adding-a-text-to-an-image-in-WP7.aspx感谢所有回复最初帮助的人! :)

最佳答案

这是将 TextBlock 文本转换为图像的简单方法

    private void convert_Click(object sender, RoutedEventArgs e)
    {
        Canvas c1 = new Canvas();
        TextBlock t = new TextBlock();
        t.Text = text1.Text;
        t.FontFamily = text1.FontFamily;
        t.Foreground = text1.Foreground;
        t.FontSize = text1.FontSize;
        c1.Children.Add(t);
        WriteableBitmap wbmp = new WriteableBitmap(c1, null);
        im = new Image();
        im.Source = wbmp;
        im.Height = 200;
        im.Width = 200;     
        Canvas.SetTop(im, 10);
        Canvas.SetLeft(im, 10);

        Main_Canvas.Children.Add(im);
    }

这里我将Textblock Text转换为Bitmap,然后赋值给图片源。

关于c# - 在 Windows Phone 7 应用程序的 c# 中将字符串转换为图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12238810/

相关文章:

windows-phone-7 - 确定当前方向

c# - 不包含 'GetAwaiter' 的定义

c# - C#中删除JavaScript创建的本地存储的方法

c# - Fluent Validation 链式规则不适用于多个 When 条件

c# - 输入语言 Hook

c# - 在 WP7 中绑定(bind) ToggleButton 的 IsChecked 属性

windows-phone-7 - 如何同时为两个轴(X,Y)做平移动画?

wcf - 读取 XML 数据时已超过最大字符串内容长度配额 (8192)

C# getter/setter 对

c# - 打开 xml sdk 不读取/计算 excel 公式单元格