繁体   English   中英

C#,将gif数据转换为二进制数组以转换为图像

[英]C# , convert gif data to binary array to image

这与测试仪器有关,但需要C#代码方面的更多帮助。 我在这里对我的问题做了更多解释。 我向仪器发送命令,而我只是从仪器接收数据。 接收到的数据是真实格式(二进制),我只是将其放入字符串变量中。 在这里,我捕获了字符串中的内容。

http://i.stack.imgur.com/UcYqV.png

那我想做的就是我要这个字符串转换字节数组。 因为我的目标是在PC中制作png文件。 仪器手册说,返回的数据是gif格式,但返回的是实型。” 我相信问题点是当我转换为字节数组时,出现了问题……有人有这种经验吗?

            /// below is just send command to instrument that i want " Returns an image of the display in .gif format "
            my6705B.WriteString("hcop:sdump:data?", true);
            string image_format = my6705B.ReadString();
            /// what's inside string image_format ??![i attached screenshot png file. this is what i received from instrument. (manual said this is Returns an image of the display in .gif format)][1] ![png file][2]  

http://i.stack.imgur.com/UcYqV.png

          /// now here i think i did something wrong,
          /// the goal is i want change this return data to gif or png or image file.
          /// any solution is ok for me
          /// i just try that change this data to byte array and then try to change image file.
         ///  i think some error here because my code success to convert byte array then create image,,, error
         //// i believe that i did something wrong in convert byte array.....

            System.Text.UnicodeEncoding encode = new System.Text.UnicodeEncoding();
            byte[] byte_array22 = encode.GetBytes(image_format);

            MemoryStream ms4 = new MemoryStream(byte_array22);
            Image image = Image.FromStream(ms4);     //// error point
            image.Save(@"C:\Users\Administrator\Desktop\imageTest.png");

我相信我的解释是有评论的。 让我再次说明,我的目标是将gif数据转换为图像文件。 工具给我们返回显示格式为.gif的图像。 我收到此数据到字符串数组。 <我不知道这是正确还是错误,但是现在我放到字符串数组中>然后我只想将此gif数据转换为png或jpg文件。

请指教,。

请尝试像

image.Save(@"C:\Users\Administrator\Desktop\imageTest.png", System.Drawing.Imaging.ImageFormat.Png);

更新:

byte[] byte_array22 = Encoding.Unicode.GetBytes(image_format);
MemoryStream ms4 = new MemoryStream(byte_array22);
Image image = Image.FromStream(ms4, true, true);
image.Save(@"C:\Users\Administrator\Desktop\imageTest.png",System.Drawing.Imaging.ImageFormat.Png);

嗯...这里有几种方法可以将图像转换为Base64格式(字符串),然后转换回来。

    private string ImageToBase64(Image image, System.Drawing.Imaging.ImageFormat format)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            // Convert Image to byte[]
            image.Save(ms, format);
            byte[] imageBytes = ms.ToArray();

            // Convert byte[] to Base64 String
            string base64String = Convert.ToBase64String(imageBytes);
            return base64String;
        }
    }

    private Image Base64ToImage(string base64String)
    {
        // Convert Base64 String to byte[]
        byte[] imageBytes = Convert.FromBase64String(base64String);
        using (var ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
        {

            // Convert byte[] to Image
            ms.Write(imageBytes, 0, imageBytes.Length);
            Image image = Image.FromStream(ms, true);
            return image;
        }
    }

您可以使用它们并与它们一起玩以查看它们是否适合您。

我不确定拍摄图像并将其简单地转储到字符串数组是否会对您有很大帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM