Victor Image Processing Library How-to Tips

Load a JPEG/GIF/TIFF/PNG/BMP Image through a Byte Array




Load a JPEG/GIF/TIFF/PNG/BMP image through a Byte Array - the C# Source Code
Windows 8 [Windows Store apps only]

This example demonstrates both the reading of the file and proper allocation of a memory buffer to hold it, and the use of the Victor library to decompress the image data and create the image. Requires Victor Image Processing Library for 32-bit Windows, v 6 or higher and the Victor Library .Net namespace viclib_w8.

        // Load an image through a byte array
        static string filepath;
        static string filename;
        async void b_fileopen_clicked(object sender, RoutedEventArgs e)// File open button clicked
        {
            byte[] filebuffer = new byte[0];
            if (this.EnsureUnsnapped())
            {

                // LoadImageFileAsync function defined below
                byte[] fb = await LoadImageFileAsync();// Load a file into a byte array
                if (fb != null)
                    filebuffer = fb;
            }
        // Load the image into an image buffer described by local variable timage
            if (filebuffer.Length > 0)
            {
                int rcode;
                vicwin_ws.imgdes timage;
                timage = new vicwin_ws.imgdes();

                // loadimagefrombytearray function defined below
                rcode = loadimagefrombytearray(ref timage, filebuffer);
                if (rcode == vicwin_ws.NO_ERROR)
                {

                    // Success, copy temp to global image, defined in Victor Brightenator Windows Store example
                    replace_global_image(ref timage);    
                    vicwin_ws.freeimage(ref timage);    // Free temp timage
                }
            }
        }
        unsafe public static int loadimagefrombytearray(ref vicwin_ws.imgdes timage, byte[] fb)
        {
            int rcode;
            int length, width, vbitcount;
            
            
        // Test for JPEG
                vicwin_ws.JpegDataEx jdata = new vicwin_ws.JpegDataEx();
                rcode = vicwin_ws.jpeginfofrombytearrayex(fb, ref jdata);
                if (rcode == vicwin_ws.NO_ERROR)
                {
                    width = (int)jdata.width;
                    length = (int)jdata.length;
                    vbitcount = jdata.vbitcount;
                    rcode = vicwin_ws.allocimage(ref timage, width, length, vbitcount);
                    if (rcode == vicwin_ws.NO_ERROR)
                        rcode = vicwin_ws.loadjpgfrombytearray(fb, ref timage);
                    goto done;
                }
        // Test for GIF
                vicwin_ws.GifData gdata = new vicwin_ws.GifData();
                rcode = vicwin_ws.gifinfofrombytearray(fb, ref gdata);
                if (rcode == vicwin_ws.NO_ERROR)
                {
                    width = (int)gdata.width;
                    length = (int)gdata.length;
                    vbitcount = gdata.vbitcount;
                    rcode = vicwin_ws.allocimage(ref timage, width, length, vbitcount);
                    if (rcode == vicwin_ws.NO_ERROR)
                        rcode = vicwin_ws.loadgiffrombytearray(fb, ref timage);
                    goto done;
                }
        // Test for TIFF
                vicwin_ws.TiffData tdata = new vicwin_ws.TiffData();
                rcode = vicwin_ws.tiffinfofrombytearray(fb, ref tdata);
                if (rcode == vicwin_ws.NO_ERROR)
                {
                    // Tiff files require special function to handle the byte array
                    width = (int)tdata.width;
                    length = (int)tdata.length;
                    vbitcount = tdata.vbitcount;
                    rcode = vicwin_ws.allocimage(ref timage, width, length, vbitcount);
                    if (rcode == vicwin_ws.NO_ERROR)
                        rcode = vicwin_ws.loadtiffrombytearray(fb, ref timage);
                    goto done;
                }
        // Test for PNG
                vicwin_ws.PngData pdata = new vicwin_ws.PngData();
                rcode = vicwin_ws.pnginfofrombytearray(fb, ref pdata);
                if (rcode == vicwin_ws.NO_ERROR)
                {
                    width = (int)pdata.width;
                    length = (int)pdata.length;
                    vbitcount = pdata.vbitcount;
                    rcode = vicwin_ws.allocimage(ref timage, width, length, vbitcount);
                    if (rcode == vicwin_ws.NO_ERROR)
                        rcode = vicwin_ws.loadpngfrombytearray(fb, ref timage);
                    goto done;
                }
        // Test for BMP
                vicwin_ws.BITMAPINFOHEADER bdata = new vicwin_ws.BITMAPINFOHEADER();
                rcode = vicwin_ws.bmpinfofrombytearray(fb, ref bdata);
                if (rcode == vicwin_ws.NO_ERROR)
                {
                    width = bdata.biWidth;
                    length = bdata.biHeight;
                    vbitcount = bdata.biBitCount;
                    rcode = vicwin_ws.allocimage(ref timage, width, length, vbitcount);
                    if (rcode == vicwin_ws.NO_ERROR)
                        rcode = vicwin_ws.loadbmpfrombytearray(fb, ref timage);
                    goto done;
                }
        done:
            return (rcode);
        }
      async public static Task<byte[]> LoadImageFileAsync()
        {
            FileOpenPicker picker = new FileOpenPicker();
            picker.FileTypeFilter.Add(".tif");
            picker.FileTypeFilter.Add(".jpg");
            picker.FileTypeFilter.Add(".gif");
            picker.FileTypeFilter.Add(".png");
            picker.FileTypeFilter.Add(".bmp");
            picker.ViewMode = PickerViewMode.Thumbnail;
            picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            StorageFile storageFile = await picker.PickSingleFileAsync();
            // If user presses Cancel, result is zero
            if (storageFile == null)
                return null;
            filepath = storageFile.Path;
            filename = storageFile.Name;
            using (IRandomAccessStream stream = await storageFile.OpenReadAsync())
            {
                using (DataReader dataReader = new DataReader(stream))
                {
                    uint length = (uint)stream.Size;
                    byte[] imagedata = new byte[length];
                    await dataReader.LoadAsync(length);
                    dataReader.ReadBytes(imagedata);
                    return (imagedata);
                }
            }
        }
Victor Image Processing Library

Victor Image Processing Library homepage | Victor Product Summary | more source code




Copyright © 2013 Catenary Systems Inc. All rights reserved. Victor Image Processing Library is a trademark of Catenary Systems.