• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by lihuyt · Oct 25, 2013 at 01:35 PM · c#arraypngbitmap

Saving Png image to MemoryStream causes Access Violation Crash

We have an application in Unity3d where we want to take in a byte array formatted in ARGB (0-255 format), convert it to a bitmap and then to a png byte array and display this as a texture2d.

All the separate parts of the system works fine, though when trying to save the bitmap to the MemoryStream in PNG imageFormat Unity Crashes and leaves us with the following error:

Unity Editor [version: Unity 4.2.1f4_4d30acc925c2]

Unity.exe caused an Access Violation (0xc0000005) in module Unity.exe at 0023:00000000.

Error occurred at 2013-10-25_152302. C:\Program Files (x86)\Unity\Editor\Unity.exe, run by Commander. 11% memory in use. 0 MB physical memory [0 MB free]. 0 MB paging file [0 MB free]. 0 MB user address space [3511 MB free]. Read from location 00000000 caused an access violation.

We have added the dll System.Drawing.dll into the main Asset Folder.

The function which causes the crash (bmp2.Save) works just fine if we set it to save the file to disk instead of to stream.

The following is the main code which tries to save the bitmap to MemoryStream:

 // Create bitmap from raw image data
 Bitmap bmp = CreateBitmap(rawImageBuffer, header);
 
 // encode byte array to png formated byte array
 byte[] resultImageBuffer = null;
                             
 using (Bitmap bmp2 = new Bitmap(bmp))
 {
     using  (System.IO.MemoryStream stream = new System.IO.MemoryStream())
     {
         // This is the line which causes the crash /////////////////////////
         bmp2.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
 
         Debug.Log("Saved to stream");
            
         resultImageBuffer = stream.ToArray();
 
         if (resultImageBuffer == null)
             Debug.Log("resultImageBuffer is null");
         else
             Debug.Log("resultImageBuffer length: " + resultImageBuffer.Length);
     }
 }



Following is the code which converts the raw image data ARGB byte array into a bitmap.

 /// <summary>
 /// Creates a Bitmap image from raw image data
 /// </summary>
 /// <param name="rawImage">Raw image data, uncompressed</param>
 /// <param name="header">Message header describing original image format</param>
 /// <returns>A bitmap</returns>
 private Bitmap CreateBitmap(byte[] rawImage, MessageHeader header)
 {
     // Create bitmap from raw image data
     Bitmap bmp = new Bitmap(header.Width, header.Height, header.PixFormat);
     BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);
     Marshal.Copy(rawImage, 0, bmpData.Scan0, rawImage.Length);
     bmp.UnlockBits(bmpData);
 
     return bmp;
 }


How can we solve the Access Violation so that it can save the bitmap successfully to the memorystream?

What is the Cause of the Access Violation Error?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Dave-Carlile · Oct 25, 2013 at 01:56 PM

If you're just trying to create a Texture2D from raw ARGB data, why wouldn't you just do something like this?

     Color32[] colors = new Color32[your_width * your_height];
     // TODO : fill in colors from your raw data

     Texture2D texture = new Texture2D(your_width, your_height, TextureFormat.ARGB32, false);
     texture.SetPixels32(colors);
     texture.Apply();


Is there a compelling reason you need to go through all the other steps?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Set default length for an array of elements of a custom class in inspector 0 Answers

How to check if a value exists in an array (C#) 3 Answers

Move files from folder a to folder b 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges