🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

MemoryStream use in Unity for System.Drawing.Image

Started by
9 comments, last by Gautti 5 years, 3 months ago

Hi, I am really confused how to load System.Drawing.Image using MemoryStream in Unity.  My code is working in Unity Editor only but I can't get system image from Windows build

here is my code,

using UnityEngine;

using System.Drawing;

 public Image byteArrayToImage(byte[] byteArrayIn)
    {
        MemoryStream ms = new MemoryStream(byteArrayIn);
        Image returnImage = Image.FromStream(ms);
        return returnImage;

    }

I want to print image from this code i have attached the printer and i got image in editor but when I build in windows, I get an error with Object reference not set to instance of an object for an issue of MemoryStream. Any help will be appreciated.

Advertisement

You know that System.Drawing is not officially supported in Unity standalone builds because it contains heavy platform depending code?

You could add a null check before using the MemoryStream if you think the error relates to the MemoryStream not being initialized properly to get if it is realy related to that or a problem of something inside the Image.FromStream function.

Also the exact error message would be helpfull.

Thanks Shaarigan for your valuable reply, But how could I get my result ? Officially i need a system image using Unity for a printing purpose. As you know we can not convert system.image from Unity UI image.

20 hours ago, Gautti said:

As you know we can not convert system.image from Unity UI image

Surre we can!

https://docs.unity3d.com/ScriptReference/ImageConversion.LoadImage.html

Unity carries it's own asset loading because handling raw image data in memory is worthless for the render engine so it has to be uploaded to the GPU

Ok Thank you Shaarigan, I got a print by saving my image file in certain folder and then I got working. By the way do you know how to get printer status like Paper Jammed, Paper Out, Start, ideal or running from Unity ? Actually I am working in printer base project and I want to show printer status before printing anything from Unity.

How to know the Thermal USB Printer Status in Unity like paper jammed, paper out etc. ?

You wont without a highly platform related extension. Unity isn't the best choice for doing "Office Work" as it is designed to provide platform independent game experiences and so just carries only game related features by design

Thanks Shaarigan, But I need to print ticket out from thermal printer. Actually I am developing a slot game and for a ticket print I just want to attach a printer. There is a functionality from it I can get the status of a printer from Unity side.

So you have certain platform set for that game or do you require for every user on every possible platform to have a printer attached?

Otherwise you know what platform this will run at and could write a Printer Plugin for that special kind of hardware by yourself and embedd it into Unity

Yes, Exactly i just wanted to get appear the status of currently attached printer from Unity to windows build before printing document.

This topic is closed to new replies.

Advertisement