/// <summary> /// Gets a <see cref="Bitmap"/> from an EDSDK pointer to an image (Jpg or Raw) /// </summary> /// <param name="imgStream">Stream pointer to the image</param> /// <param name="imageSource">The result image type</param> /// <returns>A <see cref="Bitmap"/> image from the given stream pointer</returns> protected Bitmap GetImage(IntPtr imgStream, ImageSource imageSource) { IntPtr imgRef = IntPtr.Zero; IntPtr streamPointer = IntPtr.Zero; ImageInfo imageInfo; try { //create reference and get image info ErrorHandler.CheckError(this, CanonSDK.EdsCreateImageRef(imgStream, out imgRef)); ErrorHandler.CheckError(this, CanonSDK.EdsGetImageInfo(imgRef, imageSource, out imageInfo)); Size outputSize = new Size(); outputSize.Width = imageInfo.EffectiveRect.Width; outputSize.Height = imageInfo.EffectiveRect.Height; //calculate amount of data int datalength = outputSize.Height * outputSize.Width * 3; //create buffer that stores the image byte[] buffer = new byte[datalength]; //create a stream to the buffer using (var stream = new SDKStream(buffer)) { //load image into the buffer ErrorHandler.CheckError(this, CanonSDK.EdsGetImage(imgRef, imageSource, TargetImageType.RGB, imageInfo.EffectiveRect, outputSize, stream.Reference)); //make BGR from RGB (System.Drawing (i.e. GDI+) uses BGR) unsafe { byte tmp; fixed(byte *pix = buffer) { for (long i = 0; i < datalength; i += 3) { tmp = pix[i]; //Save B value pix[i] = pix[i + 2]; //Set B value with R value pix[i + 2] = tmp; //Set R value with B value } } } //Get pointer to stream data ErrorHandler.CheckError(this, CanonSDK.EdsGetPointer(stream.Reference, out streamPointer)); //Create bitmap with the data in the buffer return(new Bitmap(outputSize.Width, outputSize.Height, datalength, PixelFormat.Format24bppRgb, streamPointer)); } } finally { //Release all data if (imgStream != IntPtr.Zero) { ErrorHandler.CheckError(this, CanonSDK.EdsRelease(imgStream)); } if (imgRef != IntPtr.Zero) { ErrorHandler.CheckError(this, CanonSDK.EdsRelease(imgRef)); } } }
/// <summary> /// Gets a thumbnail from a Raw or Jpg image /// </summary> /// <param name="filepath">Path to the image file</param> /// <returns>A <see cref="Bitmap"/> thumbnail from the provided image file</returns> public Bitmap GetFileThumb(string filepath) { //create a file stream to given file using (var stream = new SDKStream(filepath, FileCreateDisposition.OpenExisting, FileAccess.Read)) { //Create a thumbnail Bitmap from the stream return(GetImage(stream.Reference, ImageSource.Thumbnail)); } }