Main Site Documentation

Resize and save picture


#1

Hello!
(I am french)
if I resize an picture and I save it I can not read the file.


/*
picture = Camera picture
storage = sdCard.GetStorageDevice();
s_file = @ "db\img\img001.bmp"
s_file2 = @ "db\img_min\img001.bmp"

*/
try
            {
                Debug.Print("Save picture...");
                storage.WriteFile(s_file, picture.PictureData);
                Debug.Print("resize picture ...");
                Bitmap b = utile.Divsize(picture.MakeBitmap());
                Debug.Print("Save picture resize ...");
                storage.WriteFile(s_file2, b.GetBitmap());
                Debug.Print("Display picture resize ...");
                display_T35.SimpleGraphics.DisplayImage(b, 0, 0);// It is ok
                Debug.Print("load fill ...");
                byte[] _byte = storage.ReadFile(s_file2);
                Debug.Print("convert fill to picture...");
                Bitmap b2 = new Bitmap(_byte, Bitmap.BitmapImageType.Bmp);// I have try all Type
                Debug.Print("display picture loaded...");
                display_T35.SimpleGraphics.DisplayImage(b2, 0, 50);
                Debug.Print("end save and test...");
                led.TurnBlue();
            }
            catch (Exception ex)
            {
                Debug.Print("Erreur dans la sauvgarde ");
                Debug.Print("Message: " + ex.Message + "  Inner Exception: " + ex.InnerException);
                led.TurnRed();
            }


public static Bitmap Divsize(Bitmap originale)
        {
            uint D = 5;
            Bitmap sortie = new Bitmap(64, 48);
            for (int y = 0; y < sortie.Height; y++)
            {
                for (int x = 0; x < sortie.Width; x++)
                {
                    sortie.SetPixel(x, y, originale.GetPixel((int)(x * D), (int)(y * D)));
                }
            }

            return sortie;
        }


Save picture...
resize picture ...
Save picture resize ...
Display picture resize ...
load fill ...
convert fill to picture...
    #### Exception System.Exception - CLR_E_FAIL (9) ####
    #### Message: 
    #### Microsoft.SPOT.Bitmap::.ctor [IP: 0000] ####
    #### Project_V1._0._2.Program::save_laste_picture [IP: 0206] ####
Une exception de première chance de type 'System.Exception' s'est produite dans Microsoft.SPOT.Graphics.dll
Erreur dans la sauvgarde 
Message: Exception was thrown: System.Exception  Inner Exception: 
Le thread '<Sans nom>' (0x9) s'est arrêté avec le code 0 (0x0).

when I look the file in the SD card with my pc:
-> The original image is ok
-> The resize image is not readable


#2

@ Egaro555 - Firstly, you may be having issues because Bitmap in NETMF is an array of bits (bit map), not the Windows .BMP format. The program you are using is more than likely auto recognizing the format, and is able to handle opening the output from the Bitmap class. However, I would try to convert the resized image to a portable format such as jpeg, etc, to see if that clears up any issues.


#3

The problem is solved:
must use “BitmapToBMPFile”

Debug.Print("Save picture...");
                storage.WriteFile(s_file, picture.PictureData);
                Debug.Print("resize picture ...");
                Bitmap b = utile.Divsize(picture.MakeBitmap());
                Debug.Print("convert resize picture ...");
                byte[] b_byte = new byte[b.Width*b.Height*3+54];
                GHI.Premium.System.Util.BitmapToBMPFile(b.GetBitmap(), b.Width, b.Height, b_byte);
                GT.Picture b_picture = new GT.Picture(b_byte, GT.Picture.PictureEncoding.BMP);
                Debug.Print("Save picture resize ...");
                storage.WriteFile(s_file2, b_picture.PictureData);
                Debug.Print("Display picture resize ...");
                display_T35.SimpleGraphics.DisplayImage(b_picture, 0, 0);
                Debug.Print("load fill ...");
                byte[] _byte = storage.ReadFile(s_file2);
                Debug.Print("convert fill to picture...");
                Bitmap b2 = new Bitmap(_byte, Bitmap.BitmapImageType.Bmp);
                Debug.Print("display picture loaded...");
                display_T35.SimpleGraphics.DisplayImage(b2, 0, 50);
                Debug.Print("end save and test...");
                led.TurnBlue();