question

Takashi-8437 avatar image
0 Votes"
Takashi-8437 asked Takashi-8437 edited

How to convert 2D short array to base64

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!

dotnet-aspnet-webforms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LanHuang-MSFT avatar image
0 Votes"
LanHuang-MSFT answered

Hi @Takashi-8437,
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.



image.png (62.6 KiB)
image.png (9.8 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.