private void J2KViewer_Load(object sender, System.EventArgs e) { // J2K_Codec.StartLogging(); // if you need to debug... // Unlock the component. Use your personal registration key here J2K_Codec.Unlock("Put your key here"); // Show the version of J2K-Codec in the window title bar int version = J2K_Codec.GetVersion(); int major = version >> 20; int minor = (version >> 16) & 0x0F; int build = version & 0xFFFF; this.Text = string.Format("J2K Viewer (codec ver: {0}.{1}.{2})", major, minor, build); }
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; }