// Returns the textual description of an error public static string GetErrStr(J2K_Error errCode) { StringBuilder sb = new StringBuilder((int)J2K_Error.MaxErrorLen); GetErrorString(errCode, sb); return(sb.ToString()); }
private static extern void GetErrorString(J2K_Error errCode, StringBuilder errStr);
private void ShowImage(string fileName, int resolution) { // Open picture file and obtain an image handle J2K_Image img = new J2K_Image(); img.version = J2K_Codec.Version; J2K_Error err = J2K_Codec.OpenFile(fileName, ref img); if (err != J2K_Error.Success) { // If error then show the error message and exit function MessageBox.Show(J2K_Codec.GetErrStr(err), "Error"); J2K_Codec.Close(ref img); return; } // Set resolution level and update Width and Height img.resolution = resolution; J2K_Codec.GetInfo(ref img); // Show to the user the file name and size InfoLabel.Text = string.Format("{0} | {1} x {2} x {3}", new FileInfo(fileName).Name, img.Width, img.Height, img.Components); // Create a bitmap info structure BITMAPINFO bmp = new BITMAPINFO(); bmp.Size = Marshal.SizeOf(bmp); bmp.Width = img.Width; // Image width bmp.Height = img.Height; // Image height bmp.Planes = 1; bmp.BitCount = 32; // Always 4 bytes per pixel bmp.Compression = 0; // No compression (RGB format) bmp.SizeImage = 0; bmp.XPelsPerMeter = 0; bmp.YPelsPerMeter = 0; bmp.ClrUsed = 0; bmp.ClrImportant = 0; // Create the bitmap in memory. The "bits" variable will receive a pointer // to the memory allocated for the bitmap. IntPtr bits = IntPtr.Zero; IntPtr hBitmap = CreateDIBSection(IntPtr.Zero, ref bmp, 0, ref bits, IntPtr.Zero, 0); if (hBitmap == IntPtr.Zero) { // There was an error creating the bitmap J2K_Codec.Close(ref img); MessageBox.Show("Can't create bitmap - not enough memory?"); } // Decode the image into the newly created bitmap memory (pointed to by the // "bits" variable). this.Cursor = Cursors.WaitCursor; img.buffer = bits; img.buffer_bpp = 4; img.buffer_pitch = img.Width * img.buffer_bpp; err = J2K_Codec.Decode(ref img, "bmp=1"); if (err == J2K_Error.Success) { // Obtain a managed bitmap Bitmap result = Bitmap.FromHbitmap(hBitmap); // Show it in the image box pictureBox.Image = result; // Resize the window to embrace the image ResizeWindow(img.Width, img.Height); } else { // If error then show the error message MessageBox.Show(J2K_Codec.GetErrStr(err), "Error"); } // Delete the DIB to release memory DeleteObject(hBitmap); // Close the image J2K_Codec.Close(ref img); // Restore cursor this.Cursor = Cursors.Default; }