Having trouble creating a Bitmap from a gif image

Hi, its been a long time since I’ve been here. I’ve recently got the SCM2026D Dev board and wanted to try out the newer hardware with TinyCLR 2.0 (Spider was my last attempt :wink: )

I’ve got an SD card inserted an image stored in the root folder. A 24 bit Bitmap and a gif version. The bitmap loads and displays as expected using the following code:

var fileStream = new FileStream($@"{drive.Name}potion1ico.gif", FileMode.Open);
var icon = new Bitmap(fileStream);

If I try to load the .gif (Or a the .bmp) the using the new Bitmap(bytes, BitmapImageType.xxx) style:

	string iconFileName = $@"{drive.Name}potion1ico.gif";
	try
	{

		using (FileStream fsSource = new FileStream(iconFileName,
			FileMode.Open, FileAccess.Read))
		{

			// Read the source file into a byte array.
			byte[] bytes = new byte[fsSource.Length];
			int numBytesToRead = (int)fsSource.Length;
			int numBytesRead = 0;
			while (numBytesToRead > 0)
			{
				// Read may return anything from 0 to numBytesToRead.
				int n = fsSource.Read(bytes, numBytesRead, numBytesToRead);

				// Break when the end of the file is reached.
				if (n == 0)
					break;

				numBytesRead += n;
				numBytesToRead -= n;
			}
			icon = new Bitmap(bytes, BitmapImageType.Gif);
		}
	}
	catch (Exception ioEx)
	{
		Debug.WriteLine(ioEx.Message);
		icon = new Bitmap(50, 50);
	}

I get the following exception:

System.ArgumentException

Is this not supported in TinyCLR 2.0?

Thanks in advance for any help.

Jas

Does your file have to be a gif or do you have the capability of converting it? I couldn’t even get a JPG to work so every image in my project is a native bitmap.

Only 6 years! Welcome back.

Did you try it as a resource?

Hi Gus,
I’ve been eyeing the SCM20260D for a while so saved up a grabbed one :-). I still love using .net to code, even c# now ;-).
As to the resource, yes, I’ve added as a Gif Image resource it will display with no issues and correct transparency using:

var icon = BinaryResources.GetBitmap(BinaryResources.BitmapResources.potion1ico);

The issue is, I would like the software to download the required images (Plus other resources) from a cloud server to its local sd card and then display them on the screen in the app. I’m starting small so just trying to load an image from the sd card first :wink:

@sgtyar95 If I convert to a bitmap, I loose the transparency.

Cheers,

Jas

Ok now we know the problem is not in gif processing.

Next trial, when you read the image from an SD card, check the byte count and verify it matches the size you see on your desktop.

I’ve check the file on disk and it’s 1,581 bytes in size.
I set a break point just as it tries to create the bitmap and it shows that bytes = {byte[1581]}. So it looks like the size is the same.

Just to verify, I wrote the file back to the disk as a new .gif:

var file = new FileStream($@"{drive.Name}Test.gif", FileMode.OpenOrCreate);
file.Write(bytes, 0, bytes.Length);
file.Flush();

And this was the correct size and image when I checked on my laptop.

Please email your gif to support email

Did you receive the file?

Yes we have it and looking into it Graphics issue with GIF · Issue #845 · ghi-electronics/TinyCLR-Libraries · GitHub

In case anyone else hits this problem, here is a version of the code that seems to work as expected

var sd = StorageController.FromName(SC20260.StorageController.SdCard);
var drive = FileSystem.Mount(sd.Hdc);
var stream = new FileStream(drive.Name + "Test.gif", FileMode.Open);
var data = new byte[stream.Length];
stream.Read(data, 0, data.Length);
var gifBitmap = new Bitmap(data, BitmapImageType.Gif);

Thanks for the help GHI.

1 Like