-2

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?

Here is my Python code:

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 ?

LopDev
  • 823
  • 10
  • 26
Ricov
  • 1
  • 1

1 Answers1

0

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.

In Python you can save the bytes by opening a file in binary mode, then writing the bytes:

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.

dogman288
  • 613
  • 3
  • 9
  • the problem is that python server side and C# client side was totally separate, and python server side also have no permissions save images, – Ricov Jul 10 '20 at 02:57
  • 1
    You will have to send the bytes over the network then, you could [base64 encode](https://docs.python.org/3/library/base64.html) the binary data and send it as text – dogman288 Jul 10 '20 at 03:01