How to convert 2D short array to base64

Takashi Chen 1 Reputation point
2022-05-09T12:34:28.063+00:00

I have a 2D short array coming from a C++ library. The array contains the pixels of a 16 bit grayscale image. I am trying to convert it to Base64 to display it. My C# code is as follows:

            string picture_path = "~/Picture/CT_Axial_Z/CT_Axial-Z_0111.png";
            string path = System.Web.Hosting.HostingEnvironment.MapPath(picture_path);

            if (File.Exists(path))
            {

                IntPtr gpget = Open16BitGrayImage(path);
                CImage_DLL gget = (CImage_DLL)Marshal.PtrToStructure(gpget, typeof(CImage_DLL));
                int short_size = sizeof(short);
                int p_s_cursor = 0;

                Array pshImageData = Array.CreateInstance(typeof(short), gget.m_nH, gget.m_nW);

                for (int i = 0; i < gget.m_nH; i++)
                {
                    for (int j = 0; j < gget.m_nW; j++)
                    {
                        short data = (short)Marshal.PtrToStructure(gget.pshImageData + p_s_cursor, typeof(short));
                        pshImageData.SetValue(data, i, j);
                        p_s_cursor += short_size;
                    }
                }

            }

I need to convert it to base64 and display it on canvas, how should I do it?
Sorry,my english maybe not good,But hope someone can help me~Thanks!

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,282 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 25,871 Reputation points Microsoft Vendor
    2022-05-10T03:14:39.083+00:00

    Hi @Takashi Chen ,
    If you want to render the result in a canvas, maybe you can do it using JavaScript.
    Using the btoa() method, btoa() accepts a "string" where each character represents an 8-bit byte – if you pass a string containing characters that can't be represented in 8 bits, it will probably break. This isn't a problem if you're actually treating the string as a byte array, but if you're trying to do something else then you'll have to encode it first.

    You can refer to the example below.
    200419-image.png
    200457-image.png
    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments