Notification

Icon
Error

Retrieve tiny resource as a byte array
Jan Kučera
#1 Posted : Sunday, May 20, 2012 10:03:26 AM(UTC)
Rank: Newbie

Joined: 11/24/2011(UTC)
Posts: 64

Hello,


is there a way how the managed code can get a tiny resource as a byte array?


The generated ResourceManager only returns Bitmap, Font, etc. objects via the Microsoft.SPOT.ResourceUtility.GetObject method. The version with offset and length throws an InvalidOperationException if I try to get these typed resources.


If that is not possible, is there any trick how to make the compiler to attach valid tiny resources as plain byte[] resources?


Thanks,
Jan

Lorenzo Tessiore
#2 Posted : Monday, May 21, 2012 11:29:13 AM(UTC)
Lorenzo Tessiore

Rank: Tinkerer

Joined: 2/1/2010(UTC)
Posts: 36

 


NETMF provides two methods to retrieve resources using the type Microsoft.SPOT.ResourceUtility and precisely:


public static object GetObject(System.Resources.ResourceManager rm, Enum id) and


public static object GetObject(System.Resources.ResourceManager rm, Enum id, int offset, int length);


 


Both methods allow retrieving binary resources as well as bitmaps and so on..., where the second method allows retrieving binary resources in chunks.


You can also accomplish the same task by using the code auto-generated by the resource designer. See the HttpClient sample for a usage example, also reported below:


namespace HttpClientSample
{
    public static class MyHttpClient
    {
        /// <summary>
        /// Retrieves a page from a Web server, using a simple GET request.
        /// </summary>
        public static void Main()
        {
            Microsoft.SPOT.Hardware.Utility.SetLocalTime(new DateTime(2011, 4, 14));


            // Wait for DHCP (on LWIP devices)
            while (true)
            {
                IPAddress ip = IPAddress.GetDefaultLocalAddress();


                if (ip != IPAddress.Any) break;


                Thread.Sleep(1000);
            }


            // Root CA Certificate needed to validate HTTPS servers.
            byte[] ca = Resource1.GetBytes(
                Resource1.BinaryResources.VerisignCA);


            X509Certificate[] caCerts =
                new X509Certificate[] { new X509Certificate(ca) };


...


 


To attach a resource as a binary resource to your NETMF application, you need to give your resource a neutral extension, such as .bin.  If you try to attach a bitmap as a binary resource and your file as a .bmp extension, then the resource generator will treat it as a bitmap.


 


Regards


 

Jan Kučera
#3 Posted : Monday, May 21, 2012 11:46:26 AM(UTC)
Rank: Newbie

Joined: 11/24/2011(UTC)
Posts: 64

Well but I cannot choose what the ResourceUtility returns, can I?


Imagine I added a bitmap, and I want to get this resource as an array of bytes in the MF application. How would I do that?


Jan

Olaf Herrmann
#4 Posted : Wednesday, May 23, 2012 5:09:27 AM(UTC)
Olaf Herrmann

Rank: Tinkerer

Joined: 8/25/2010(UTC)
Posts: 14

You can try to add your resource into the FILE section of the *.resx file.


Then you see in *.Designer.cs:


 


        internal static byte[] GetBytes(Resource1.BinaryResources id)
        {
            return ((byte[])(Microsoft.SPOT.ResourceUtility.GetObject(ResourceManager, id)));
        }


        [System.SerializableAttribute()]


        internal enum BinaryResources : short
        {
            ...
        }


 


Hope this helps,


Olaf

Robert Ridenour
#5 Posted : Wednesday, May 23, 2012 8:34:52 AM(UTC)
Robert Ridenour

Rank: Tinkerer

Joined: 1/28/2011(UTC)
Posts: 25

I ran into a similar problem wanting to embedded .wav files using the Resource manager. I renamed the .wav files to .dat and Resource manager created BinaryResource references for the .dat files and provided a GetBytes() method for retrieving the file data. For some reason the Resource File Generator Tool failed when including the file as .wav.

Also if it is specifically Bitmap byte[] you are looking for, a Bitmap class has a method GetBitmap that returns the Bitmap as byte[] data type.

Hope this helps,

Robert
Jan Kučera
#6 Posted : Wednesday, July 18, 2012 12:17:33 PM(UTC)
Rank: Newbie

Joined: 11/24/2011(UTC)
Posts: 64

Olaf, adding the resource as file does not help, it gets processed by the tiny resource processor anyway. And even if it worked, it would require me to include the resources twice, which usually is not desired...

Robert, the generator for .NET MF can process only strings, bitmaps, or byte arrays. Sounds are passed to the generator as MemoryStream and thus the failure. (I believe it won't be happy with the icon resources as well). This could be of course easily fixed in the SDK if requested by the users.

The GetBimap is a nice try, however it does not work on bitmap formats unavailable to the managed code, and the similar trick is not available with other processed resources, like strings or fonts...

Jan Kučera
#7 Posted : Wednesday, July 18, 2012 12:43:39 PM(UTC)
Rank: Newbie

Joined: 11/24/2011(UTC)
Posts: 64

Digging more into the framework PK, this is not possible. The GetObject with offset and length has the potential if checks were removed, but there is no way to find the resource length at runtime. Thanks for your tips!
Rss Feed  Atom Feed
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Powered by YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.073 seconds.