Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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
0
Question by unity_fiS-X9HAH5Am3A · Jan 29, 2020 at 03:20 PM · ui imagesqlitebytearrayblob

loading image as a blob from Sqlite,loading image from Sqlite

Hi guys, this is my first time using sqlite, i managed to display the text, but the image shows a red question mark, which means that the texture couldn't be retrieved, however the byte array is not empty, so i dont know what to do.

Here is my code for reading the database :

 private void readQuestionsFromDB(){
 
     string conn = "URI=file:" + Application.dataPath + "/quizdb.s3db"; //Path to database.
  IDbConnection dbconn;
  dbconn = (IDbConnection) new SqliteConnection(conn);
  dbconn.Open(); //Open connection to the database.
  IDbCommand dbcmd = dbconn.CreateCommand();
  string sqlQuery = "SELECT id, statement, answer, image " + "FROM questions";
  dbcmd.CommandText = sqlQuery;
  IDataReader reader = dbcmd.ExecuteReader();
 
  while (reader.Read())
  {
     string statement = reader.GetString(1);
 
     answerint = reader.GetInt32(2);
 
     byte[] img = (byte[])reader["image"];
 
     Question q = new Question(statement, answer, img);
     questions.Add(q);
 
  }
  reader.Close();
  reader = null;
  dbcmd.Dispose();
  dbcmd = null;
  dbconn.Close();
  dbconn = null;
 
 }


And here is how i try to display it as an image in the start method :

 readQuestionsFromDB();  
 
    statement.text = questions[0].statement;
 
    Texture2D tex = new Texture2D(800,400); //image is 800/400
     tex.LoadImage(questions[0].image);
     image.GetComponent<Image>().sprite = Sprite.Create(tex, new Rect(0,0,tex.width,tex.height),new Vector2(0.5f, 0.5f));


Here is what i get, the text displays correctly, but not the image :

alt text

Please help me, i have looked everywhere and can't find a solution, thank you in advance!

capture.png (45.5 kB)
capture.png (45.5 kB)
Comment
Add comment · Show 2
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
avatar image sacredgeometry · Jan 30, 2020 at 08:13 AM 0
Share

I know its not what you asked but: Is there any reason you need to store the image in the database? Its generally considered bad form (unless you are dealing with a lot of data that needs to be directly queryable). Can you not ins$$anonymous$$d just store the path to it and then store it on disk?

avatar image sacredgeometry · Jan 30, 2020 at 08:18 AM 0
Share

That said whats your code for writing them into the database?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Jan 29, 2020 at 03:59 PM

What kind of "image" did you store in your database? You know that Unity can only load jpg or png files natively at runtime.


Besides that what have you already done to debug your issue? Have you checked if whatever you get back from reader["image"] is actually not null and that the byte array has a non zero size? Likewise tex.LoadImage now returns a bool if the loading was successful or not.


ps: There's no need / point in creating an empty Texture that is that large when you use LoadImage. The whole content is replaced by the loaded image (if the loading is successful or course). The "question mark" texture generally indicates an error when loading the image. So either your data is corrupted in some way (no data, wrong data) or you use a non supported texture format to begin with.

Comment
Add comment · Show 4 · 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
avatar image Bunny83 · Jan 29, 2020 at 04:28 PM 0
Share

Note that for any other image format you have to do the decoding / loading yourself. The Unity engine does not include any loaders for all those file formats which the Untiy editor supports. It would blow up the size of your build and also might cause licencing issues with the used loading code.


If that's your actual issue that you want to load a format not supported by Unity, have a look at this question. You may also want to have a look at my Utilities repository. I haven't really worked on those for quite some time. Though I have a working B$$anonymous$$P and GIF loading routine. Though while the B$$anonymous$$P loader can give give you a Texture2D, the Gif loader does not (yet) directly outputs a Texture2D. Especially since GIF could contain multiple layers / frames which might be animated. Each GIFImageBlock has a DrawTo method which allows you to draw that frame to a Color32 array which you can then convert to a Texture2D.


Since my image loaders are not quite finished yet they are inside the WorkInProgress folder. If the file you try to load has any other format, I'm sorry but I don't have a solution for that. In the past I saw a paid asset on the assetstore which provides a huge plugin for loading all sorts of image formats. I think it was based on some common C++ library. Though currently I can't really find anything useful in the store.

avatar image unity_fiS-X9HAH5Am3A · Jan 29, 2020 at 05:30 PM 0
Share

i checked the size of the array, and also the boolean returned by : tex.LoadImage, here is the result :

 statement.text = questions[0].statement;
        Texture2D tex = new Texture2D(2,2);
        byte[] byt = questions[0].image;
         tex.LoadImage(byt);
        Debug.Log("array size  = "+byt.Length);
         Debug.Log("tex load image " +tex.LoadImage(byt));
         tex.Apply();
         image.GetComponent<Image>().sprite = Sprite.Create(tex, new Rect(0,0,800,400),new Vector2(0f, 0f));

![alt text][1]

The image file is .jpeg , i guess it's the same as jpg??

capture2.png (13.3 kB)
capture2.png (13.3 kB)
avatar image unity_fiS-X9HAH5Am3A · Jan 29, 2020 at 05:34 PM 0
Share

When i changed the size of the texture to :

 Texture2D tex = new Texture2D(2,2);

and this to display the image :

 image.GetComponent<Image>().sprite = Sprite.Create(tex, new Rect(0,0,800,400),new Vector2(0f, 0f));


i get an error : ArgumentException: Could not create sprite (0, 0, 800, 400) from a 8x8 texture. UnityEngine.Sprite.Create (UnityEngine.Texture2D texture, UnityEngine.Rect rect, UnityEngine.Vector2 pivot, System.Single pixelsPerUnit, System.UInt32 extrude, UnityEngine.Sprite$$anonymous$$eshType meshType, UnityEngine.Vector4 border, System.Boolean generateFallbackPhysicsShape) (at :0)

alt text

capture3.png (10.5 kB)
avatar image Bunny83 unity_fiS-X9HAH5Am3A · Jan 29, 2020 at 10:47 PM 1
Share

If it's a valid jpg / jpeg it would work. $$anonymous$$ay I ask If you can actually share the image you use? You could temporarily add this line to your code to save the byte array to a file. First you should check if you can actually open that file with an image viewer / editor.

 System.IO.File.WriteAllBytes("C:\\Testimage.jpg",  byt);

This should create a file on your C:\ drive called Testimage.jpg. Try open it. If it doesn't work you might want to share it here so we can have a look at it.


ps: The return value of LoadImage is not reliable as you can read here and here. Strangely Unity closed the issue with the answer "by design" which is I$$anonymous$$HO a horrible design. In the past people always wanted to know when the loading of an image failed and the only solution back then was to compare that 8x8 question mark image manually. It seems that is still the case.


So I'm pretty sure your image is either not a jpeg or it is corrupted in some way. How did you actually "put them in" your database?

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.

Follow this Question

Answers Answers and Comments

121 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Image showing as question mark after loading from SQLite database 0 Answers

LoadImage not loading full image,LoadImage not working properly 0 Answers

SQLite, BLOB and UI Image 2 Answers

Storing blob in SQLite 1 Answer

Array to blob 0 Answers


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