Page 1 of 1

Silverlight SaveAsString and BitmapImage

Posted: Mon Jul 08, 2013 1:24 pm
by EMDLG71
Dear Sir,
I am using gdpicture dot net 9 inside a silverlight control.
I am trying to use the string data obtained from the SaveAsString function into a Image control using the following code:

...

Code: Select all

                   string MS;

                    MS=oGdPictureImaging.SaveAsString(ImageID, 4, 0);
                    UTF8Encoding unen = new UTF8Encoding();
                    byte[] bArray=unen.GetBytes(MS);
                  
                    using (Stream s = GenerateStreamFromString(bArray))
                    {
                        s.Seek(0, SeekOrigin.Begin);
                        BitmapImage b = new BitmapImage();

                        b.SetSource(s);
                        PagePreview.Source = b;

                    }

public Stream GenerateStreamFromString(byte[] s)
        {
            MemoryStream stream = new MemoryStream();
            for (int i = 0; i < s.Length; i++)
            {
                stream.WriteByte(s[i]);
            }
          
            stream.Position = 0;
            return stream;
        }
No errors are generated but no image is created
I tried tu use the Unicode encoding as well with the same result

Thank you in advance

Re: Silverlight SaveAsString and BitmapImage

Posted: Tue Jul 23, 2013 1:05 pm
by Loïc
Hello,

First you should use the SaveAsByteArray() method since you are trying to obtain data stored in this format.

That said, the string data returned by GdPicture is encoded with Codepage Windows-1252.

So I give you compliant code in order to do conversion from / to array of bytes:

Code: Select all

        public static byte[] GetBytes(string data)
        {
            return Encoding.GetEncoding(1252).GetBytes(data);
        }

        public static byte[] GetBytes(string data, int index, int count)
        {
            byte[] b = new byte[count];
            Encoding.GetEncoding(1252).GetBytes(data, index, count,b, 0);
            return b;
        }
Hope this helps!

With best regards,

Loïc

Re: Silverlight SaveAsString and BitmapImage

Posted: Tue Jul 23, 2013 3:42 pm
by EMDLG71
Many Thanks

Emilio