Search Unity

Convert Bitmap to bytes return null when executing on build

Discussion in 'Scripting' started by plagas36, Sep 12, 2019.

  1. plagas36

    plagas36

    Joined:
    Sep 3, 2015
    Posts:
    13
    I have a pretty simple piece of code that takes a ScreenShot and then convert it to bytes to make screen sharing. It works well when using it on Unity but as soon as I try on a built .exe. I have a NullPointerException even though none of the variables are null. Can you help me ?

    I've tried a lot of different pieces of code to convert the bitmap in bytes but all give the same error. See the two version I've tried in the code below.

    First the screenshot function
    Code (CSharp):
    1. public static Bitmap TakeScreenshot()
    2. {
    3.      Rectangle totalSize = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
    4.  
    5.      Bitmap screenShotBMP = new Bitmap(totalSize.Width, totalSize.Height, PixelFormat.Format32bppArgb);
    6.  
    7.     System.Drawing.Graphics screenShotGraphics = System.Drawing.Graphics.FromImage(screenShotBMP);
    8.  
    9.     screenShotGraphics.CopyFromScreen(totalSize.X, totalSize.Y, 0, 0, totalSize.Size,CopyPixelOperation.SourceCopy);
    10.  
    11.     screenShotGraphics.Dispose();
    12.  
    13.     return
    14. }
    Then my simplest code creating the stream
    Code (CSharp):
    1. Bitmap bitmap = TakeScreenshot();
    2. ImageConverter converter = new ImageConverter();
    3. bytes = converter.ConvertTo(bitmap, typeof(byte[])) as byte[];
    4. bitmap.Dispose();
    And I've tried something like this because I wanted to change the quality
    Code (CSharp):
    1. using (Bitmap bitmap = TakeScreenshot())
    2. {
    3.     using (MemoryStream stream = new MemoryStream())
    4.     {
    5.         ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
    6.         // Create an Encoder object based on the GUID
    7.         // for the Quality parameter category.
    8.         System.Drawing.Imaging.Encoder myEncoder =                                    System.Drawing.Imaging.Encoder.Quality;
    9.  
    10.         // Create an EncoderParameters object.
    11.         // An EncoderParameters object has an array of EncoderParameter
    12.         // objects. In this case, there is only one
    13.         // EncoderParameter object in the array.
    14.         EncoderParameters myEncoderParameters = new EncoderParameters(1);                              
    15.         EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, screenCaptureQuality);                              
    16.  
    17.         myEncoderParameters.Param[0] = myEncoderParameter;                              
    18.         bitmap.Save(stream,jpgEncoder,myEncoderParameters);
    19.         bytes = stream.ToArray();
    20.      }                          
    21. }
    It works well on Unity but not when built. I feel like it's a dll problem but I don't really know what I should try to fix.

    Here is the error I get.
    Code (CSharp):
    1. Uploading Crash Report NullReferenceException: Object reference not set to an instance of an object
    2.   at System.Drawing.ComIStreamMarshaler+ManagedToNativeWrapper..cctor () [0x00049] in <7649986197334871a851fbd08bc03690>:0
    3. Rethrow as TypeInitializationException: The type initializer for 'ManagedToNativeWrapper' threw an exception.
    4.   at System.Drawing.ComIStreamMarshaler.MarshalManagedToNative (System.Object managedObj) [0x00000] in <7649986197334871a851fbd08bc03690>:0
    5.   at (wrapper managed-to-native) System.Drawing.GDIPlus.GdipSaveImageToStream(System.Runtime.InteropServices.HandleRef,System.Runtime.InteropServices.ComTypes.IStream,System.Guid&,System.Runtime.InteropServices.HandleRef)
    6.   at System.Drawing.Image.Save (System.IO.Stream stream, System.Drawing.Imaging.ImageCodecInfo encoder, System.Drawing.Imaging.EncoderParameters encoderParams) [0x0007e] in <7649986197334871a851fbd08bc03690>:0
    7.   at System.Drawing.Image.Save (System.IO.Stream stream, System.Drawing.Imaging.ImageFormat format) [0x00029] in <7649986197334871a851fbd08bc03690>:0
    8.   at (wrapper remoting-invoke-with-check) System.Drawing.Image.Save(System.IO.Stream,System.Drawing.Imaging.ImageFormat)
    9.   at System.Drawing.ImageConverter.ConvertTo (System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, System.Object value, System.Type destinationType) [0x00055] in <7649986197334871a851fbd08bc03690>:0
    10.   at System.ComponentModel.TypeConverter.ConvertTo (System.Object value, System.Type destinationType) [0x00000] in <d465e2b2e5054d2787d6364114c43446>:0
    11.   at DuneGestion.Messenger.CallController+<CallInProgress>d__21.MoveNext () [0x00184] in D:\UnityProject\DuneMessenger\Assets\Scripts\DarkRiftServer\CallController.cs:154
    12.   at UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) [0x00027] in <4d22bb06641f46818a5212b4c17a7772>:0
    Thanks and regards