3

i want to convert a string entered by user to an image..how can it be done? i tried the following code but i get an argument exception in the line : 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());

        }

    }

I got what i wanted in the following links.. How can I render text on a WriteableBitmap on a background thread, in Windows Phone 7? and http://blogs.u2u.be/michael/post/2011/04/20/Adding-a-text-to-an-image-in-WP7.aspx Thanks to all those who replied for the initial help! :)

Community
  • 1
  • 1
shubhi1910
  • 117
  • 2
  • 11
  • I'm confused, what's the goal? The user will enter a long sequence non-human-readable bytes to convert to JPG? Or are you trying to do something like have them enter "Hello" and you create an image with that text? Two completely different things. – Jim O'Neil Sep 02 '12 at 19:02
  • 1
    Something like have them enter "Hello" and you create an image with that text. Thats the goal. – shubhi1910 Sep 02 '12 at 19:11
  • 3
    ah ok, then you're quite a ways off :) You basically need to use internal drawing APIs to accomplish what you want - this reference may be a start: http://writeablebitmapex.codeplex.com/ – Jim O'Neil Sep 02 '12 at 19:28
  • hey, can you please give me an example for doing it using writeablebitmapex. – shubhi1910 Sep 03 '12 at 08:31

2 Answers2

2

It is the simple way you can convert TextBlock Text into Image

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

Here I convert the Textblock Text into Bitmap and then assign it to the image source.

selvam
  • 1,177
  • 4
  • 18
  • 40
1

Here is how to writte a string to a bitmap:

        Bitmap b = new Bitmap(200, 100);
        Graphics g = Graphics.FromImage(b);
        g.DrawString("My sample string", new Font("Tahoma",10), Brushes.Red, new Point(0, 0));
        b.Save("mypic.png", System.Drawing.Imaging.ImageFormat.Png);
        g.Dispose();
        b.Dispose();

Shubhi1910 let me know if you need any details to be explained.

Gregor Primar
  • 6,759
  • 2
  • 33
  • 46