3

I've got some C# code in Unity that grabs a large image from my Android Device's filesystem, and now with it I want to use it to create a small thumbnail image.

I've found lots of different suggestions for how to do this such as the following:

MemoryStream outputStream = new MemoryStream();
System.Drawing.Image image = System.Drawing.Image.FromFile(originalImagePath);
System.Drawing.Image thumbnail = image.GetThumbnailImage(thumbnailWidth, thumbnailHeight,()=>false, IntPtr.Zero);
thumbnail.Save(outputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
return outputStream;

However, as with the above method, all of the methods I've found require you to use the System.Drawing namespace. And I can't for the life of me get the functions in this namespace to work on Android, because even after adding "System.Drawing.dll" into the Assets folder, I get an error saying that it can't locate "gdiplus.dll" on construction of "System.Drawing.Image". I tried downloading and adding said "gdiplus.dll" to Assets, but I just get the same error as if it can't find it!

I don't understand why its so hard to get the System.Drawing functions working in Unity, but that's somewhat besides the point, as all I really want to do is create a thumbnail of an image that lives on the user's Android Device. Any suggestions would be welcome!

EDIT: I forgot to mention that I'd like to avoid solutions that use Texture2D's because they can't be run off the main thread, and hence come with performance consequences =(

Thanks in advance! =)

1 Answer 1

3

Don't use Texture2D.Resize, because the resulting texture will become grey:

After resizing, texture pixels will be undefined.

See this post for other solutions.

Old answer

I recommend use an opensource C# image processing library, such as ImageSharp or search it on GitHub.

The last solution is to write one yourself, if the performance or size of a 3rd-party library is still not good enough.

Here are two ideas I can come up with:

  1. split the possibly lengthy reading and resizing procedure into several Coroutines.
  2. use multi-thread

You may also try calling java functions from Unity3D. But I'm not familiar with that.

8
  • Hi @zwcloud, thanks for the response! So I guess I didn't tell you the full story, but using Texture2D's is a problem for me because my images are very large and hence creating them at runtime has a hit on performance... I have a method that uses a a C++Plugin to remove this cost and returns a Texture2D through CreateExternalTexture() - unfortunately calling Resize() on Texture2D's created through this method doesn't seem to work =/
    – Arthur
    Mar 24, 2017 at 12:38
  • 2
    In the end I managed to get a Java Plugin working, where I called a function from it on a separate thread that made use of android's "android.media.ThumbnailUtils" library. Since its on a separate thread it didn't cause any frame-rate hiccups either! I'm accepting your answer because it pointed me towards using Java to solve the problem!
    – Arthur
    Apr 3, 2017 at 20:19
  • Glad to know it helps!
    – zwcloud
    Apr 4, 2017 at 1:50
  • I upvoted this already but realized that tex.Resize() only changes the size and all pixels set to grey=undefined. check the document: docs.unity3d.com/ScriptReference/Texture2D.Resize.html
    – tjPark
    Nov 19, 2017 at 13:02
  • 1
    Actually, there are solutions that do not require full library like this one : blog.collectivemass.com/2014/03/resizing-textures-in-unity Tested and working for me. Most likely platform independent as it uses Texture2D class.
    – Everts
    Apr 15, 2018 at 17:39

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.