简体   繁体   English

有一个 python 字节(来自 img),我如何在 C# 中处理它,并转换为字节 [] 或图像 stream?

[英]There is a python bytes( from img) , how can I handle it in C# ,and convert to a byte[ ] or a image stream?

Now I got a python bytes like this: b'\x00\x00\x00\x00\x07\x80\x00\x03' , and it's from an image, how can I handle it in C#, and convert to a byte[ ] and then return it as a Bitmap ?现在我得到了一个像这样的 python 字节: b'\x00\x00\x00\x00\x07\x80\x00\x03' ,它来自图像,我如何在 C# 中处理它,并转换为byte[ ]然后将其作为Bitmap返回?

Here is my Python code:这是我的 Python 代码:

fig = plt.figure() 
canvas = fig.canvas buffer = io.BytesIO() 
canvas.print_png(buffer) 
bin_data = buffer.getValue() 
buffer.close() 

#Excuting reults like this:
bin_data : b'\x00\x00\x00\x00\x07\x80\x00\x03'  # just a sample 
list(bin_data) :[0, 0, 0, 0, 7, 128, 0, 3] # sample 

Is there any way to handle it in C#(client) and transform bin_data/list(bin_data) to a C# byte[] or an image Stream?有没有办法在 C#(client) 中处理它并将bin_data/list(bin_data)转换为 C# byte[]或图像 Stream?

There are a few ways to do this.有几种方法可以做到这一点。 The easiest is probably to save the bytes to a file, then load that file in C# with something like ReadAllBytes .最简单的方法可能是将字节保存到文件中,然后使用类似ReadAllBytes的内容将该文件加载到 C# 中。

In Python you can save the bytes by opening a file in binary mode, then writing the bytes:在 Python 中,您可以通过以二进制模式打开文件然后写入字节来保存字节:

f = open("my_file_name.bin", "wb")
f.write(bin_data)

Then you could use Python's subprocess to invoke your C# application, or follow along here if you want to integrate your C# code directly into your Python application.然后,您可以使用 Python 的subprocess进程来调用您的 C# 应用程序,或者如果您想将 C# 代码直接集成到您的 Python 应用程序中,请按照此处操作。

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

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