简体   繁体   English

Bitmap.Save()方法在Unity源中不起作用

[英]Bitmap.Save() method is not working in Unity source

I wanna implement 'Bitmap to Texture2D' function in Unity. 我想在Unity中实现“ Bitmap to Texture2D”功能。

I learned to make Texture2D instance, use this syntax below. 我学会了制作Texture2D实例,在下面使用此语法。

// **LoadImage(byte[] arr)** can load byte array data and make Texture2D
var data = BitmapToByteMethod(bitmap);

var textureCanvas = new Texture2D(bitmap.Width, bitmap.Height, TextureFormat.ARGB32, false);
textureCanvas.LoadImage(data);
textureCanvas.Apply();

To make this function I did like this. 为了实现此功能,我确实这样做了。

using (MemoryStream memoryStream = new MemoryStream())
{
    ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);

    System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
    EncoderParameters myEncoderParameters = new EncoderParameters(1);
    EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 100L);
    myEncoderParameters.Param[0] = myEncoderParameter;

// this is the point error occurred.

    bitmap.Save(memoryStream, jpgEncoder, myEncoderParameters);
    //bitmap.Save(memoryStream, ImageFormat.Jpeg);
    return memoryStream.ToArray();
}

First, I have a bitmap image. 首先,我有一个位图图像。 Second, Convert Bitmap to byte array. 其次,将位图转换为字节数组。 (It works well in my c# sample project.) But when I try to run that function in Unity, Unity program shut down after do 'Bitmap.Save()' method. (它在我的c#示例项目中效果很好。)但是,当我尝试在Unity中运行该函数时,执行“ Bitmap.Save()”方法后Unity程序将关闭。

I'm beginner of Unity so this is not easy to solve the problems.. below is the source sample code in my project. 我是Unity的初学者,所以这不容易解决问题。..下面是我项目中的源代码示例。

System.Drawing is not supported by Unity3D. Unity3D不支持System.Drawing Probably because it's based on GDI+ and Unity is multiplatform, so it couldn't be used anywhere but on Windows (Mono has its own version of GDI+ for *nix systems, but it doesn't work with Unity either). 可能是因为它基于GDI +,并且Unity是多平台的,所以只能在Windows上使用(Mono对于* nix系统具有自己的GDI +版本,但也不能与Unity一起使用)。 The official reason seems to be "System.Drawing doesn't work with OpenGL/DirectX". 官方原因似乎是“ System.Drawing在OpenGL / DirectX中不起作用”。

There seem to be some hacks you could use to make this work, but it only makes sense if you want to stick to Windows. 您似乎可以使用一些技巧来完成这项工作,但是只有当您希望使用Windows时才有意义。 If you want to go this way, there's an answer on the Unity3D forums that should help you - http://answers.unity3d.com/answers/253571/view.html 如果你想要走这条路,有一个在Unity3D论坛的答案,应该帮助你- http://answers.unity3d.com/answers/253571/view.html

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

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