【问题标题】:C# , convert gif data to binary array to imageC#,将gif数据转换为二进制数组到图像
【发布时间】:2014-12-10 12:30:12
【问题描述】:

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

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

那么我想要做的是我想要这个字符串转换字节数组。因为我的目标是在我的电脑中制作 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 文件。

请指教。

约瑟夫·崔

【问题讨论】:

  • 您需要读取 ASCII 字符串,而不是 16 位 unicode。当前 encode.GetBytes(image_format) 返回类似于 { '#','\0','0','\0','G','\0','I','\0',' F','\0','8','\0','9','\0' ... } 您要么必须使用其他 API,要么使用 Encoding.ASCII.GetBytes( )。
  • 我现在可以做的一件事是仪器数据格式是 ASC,所以我可以更改为 REAL 格式。那我们可以换一种方式吗??
  • 我认为关键是仪器可以返回 asc 或 real 格式。所以我所做的是将格式设置为 asc,然后更改 Encoding.ASCII.GetBytes()。但 MemoryStream ms4 = new MemoryStream(byte_array22); 中的错误Image image = Image.FromStream(ms4, true, true);

标签: c# bytearray screenshot gif imagemagick-convert


【解决方案1】:

请尝试 ImageFormat 喜欢

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

【讨论】:

  • 但就在该代码出错之前。图像图像 = Image.FromStream(ms4); //// 错误点==> 所以我认为关键点是返回字符串,我们如何处理它......
  • 是的,我确实更新了代码,但在“Image image = Image.FromStream(ms4, true, true);”中仍然出现同样的错误所以上面的评论 nitrocaster 说需要转换 ASCII 字符串,所以现在我正在尝试这个
  • 我现在可以做的一件事是仪器数据格式是 ASC,所以我可以更改为 REAL 格式。那我们可以换一种方式吗??
【解决方案2】:

嗯...这里有几种方法可以将图像转换为 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;
        }
    }

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

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

【讨论】:

  • 谢谢,我需要的是 Image Base64ToImage.. 我需要的是返回字符串可以是 asc 或 real,这意味着我可以选择 .... 我应该使用哪种方式? ?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-10
  • 1970-01-01
  • 1970-01-01
  • 2019-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多