如何使用佳能相机库拍摄照片,并在pictureBox中显示拍摄的图像照片?正在获取错误

本文关键字:照片 显示 图像 取错误 获取 并在 何使用 相机 pictureBox | 更新日期: 2023-09-27 18:26:33

这里有一个解决方案:

最简单的方法是在下载图像时订阅一个事件。要执行此操作,请转到SDKHandler类、区域Custom Events和此事件:隐藏复制代码公共事件ImageUpdate ImageReady;然后在区域Eventhandling中,在ObjectEvent_DirItemRequestTransfer的情况下,方法Camera_SDKObjectEvent,替换此隐藏复制代码

DownloadImage(inRef, ImageSaveDirectory);

这样:隐藏复制代码

if (ImageReady != null) ImageReady(DownloadImage(inRef));

因此,您不需要将拍摄的图像下载到hd,而是将其下载到图像中并启动ImageReady事件。

现在,您所要做的就是在MainForm初始化时订阅事件:隐藏复制代码

CameraHandler.ImageReady += new SDKHandler.ImageUpdate(SDK_ImageReady);

(只需将其放在其他事件订阅行下)现在您可以使用此方法发生事件:隐藏复制代码

private void SDK_ImageReady(Image img)
{
   LiveViewPicBox.Image = img;
   //you may want to save the image to the hd too. it would work like this:
   //img.Save(@"C:'Path'To'Save'image1.jpg");
}

但是我不能通过第一步,我犯了错误。

这是我在自定义事件区域所做的:

#region Custom Events
        public delegate void CameraAddedHandler();
        public delegate void ProgressHandler(int Progress);
        public delegate void StreamUpdate(Stream img);
        public delegate void BitmapUpdate(Bitmap bmp);
        public delegate void ImageUpdate(IntPtr objects);
        /// <summary>
        /// Fires if a camera is added
        /// </summary>
        public event CameraAddedHandler CameraAdded;
        /// <summary>
        /// Fires if any process reports progress
        /// </summary>
        public event ProgressHandler ProgressChanged;
        /// <summary>
        /// Fires if the live view image has been updated
        /// </summary>
        public event StreamUpdate LiveViewUpdated;
        /// <summary>
        /// If the camera is disconnected or shuts down, this event is fired
        /// </summary>
        public event EventHandler CameraHasShutdown;
        /// <summary>
        /// If an image is downloaded, this event fires with the downloaded image.
        /// </summary>
        public event BitmapUpdate ImageDownloaded;
        public event ImageUpdate ImageReady;
        #endregion

我添加了行:

public delegate void ImageUpdate(IntPtr objects);
public event ImageUpdate ImageReady;

然后我像他在SDKObjectEvent:中的解决方案中所说的那样切换了线路

private uint Camera_SDKObjectEvent(uint inEvent, IntPtr inRef, IntPtr inContext)
        {
            //handle object event here
            switch (inEvent)
            {
                case EDSDK.ObjectEvent_All:
                    break;
                case EDSDK.ObjectEvent_DirItemCancelTransferDT:
                    break;
                case EDSDK.ObjectEvent_DirItemContentChanged:
                    break;
                case EDSDK.ObjectEvent_DirItemCreated:
                    if (DownloadVideo) { DownloadImage(inRef, ImageSaveDirectory); DownloadVideo = false; }
                    break;
                case EDSDK.ObjectEvent_DirItemInfoChanged:
                    break;
                case EDSDK.ObjectEvent_DirItemRemoved:
                    break;
                case EDSDK.ObjectEvent_DirItemRequestTransfer:
                    if (ImageReady != null) ImageReady(DownloadImage(inRef));
                    break;
                case EDSDK.ObjectEvent_DirItemRequestTransferDT:
                    break;
                case EDSDK.ObjectEvent_FolderUpdateItems:
                    break;
                case EDSDK.ObjectEvent_VolumeAdded:
                    break;
                case EDSDK.ObjectEvent_VolumeInfoChanged:
                    break;
                case EDSDK.ObjectEvent_VolumeRemoved:
                    break;
                case EDSDK.ObjectEvent_VolumeUpdateItems:
                    break;
            }
            return EDSDK.EDS_ERR_OK;
        }

我切换到的线路是:

if (ImageReady != null) ImageReady(DownloadImage(inRef));

在线路上,我在右侧遇到错误:

ImageReady(DownloadImage(inRef));

错误为:

错误3参数1:无法从"void"转换为"System.IntPtr"

但是如果我要从ImageUpdate中删除参数:

public delegate void ImageUpdate();

现在我在同一行遇到错误:

if (ImageReady != null) ImageReady(DownloadImage(inRef));

这次错误也在右边说:

错误2委派"ImageUpdate"不接受1个参数

所以我被困在这里了。

这是DownloadImage方法:

/// <summary>
        /// Downloads a jpg image from the camera into a Bitmap. Fires the ImageDownloaded event when done.
        /// </summary>
        /// <param name="ObjectPointer">Pointer to the object. Get it from the SDKObjectEvent.</param>
        public void DownloadImage(IntPtr ObjectPointer)
        {
            //get information about image
            EDSDK.EdsDirectoryItemInfo dirInfo = new EDSDK.EdsDirectoryItemInfo();
            Error = EDSDK.EdsGetDirectoryItemInfo(ObjectPointer, out dirInfo);
            //check the extension. Raw data cannot be read by the bitmap class
            string ext = Path.GetExtension(dirInfo.szFileName).ToLower();
            if (ext == ".jpg" || ext == ".jpeg")
            {
                SendSDKCommand(delegate
                {
                    Bitmap bmp = null;
                    IntPtr streamRef, jpgPointer = IntPtr.Zero;
                    uint length = 0;
                    //create memory stream
                    Error = EDSDK.EdsCreateMemoryStream(dirInfo.Size, out streamRef);
                     //download data to the stream
                     lock (STAThread.ExecLock) { DownloadData(ObjectPointer, streamRef); }
                     Error = EDSDK.EdsGetPointer(streamRef, out jpgPointer);
                     Error = EDSDK.EdsGetLength(streamRef, out length);
                     unsafe
                     {
                         //create a System.IO.Stream from the pointer
                         using (UnmanagedMemoryStream ums = new UnmanagedMemoryStream((byte*)jpgPointer.ToPointer(), length, length, FileAccess.Read))
                         {
                             //create bitmap from stream (it's a normal jpeg image)
                             bmp = new Bitmap(ums);
                         }
                     }
                     //release data
                     Error = EDSDK.EdsRelease(streamRef);
                    //Fire the event with the image
                     if (ImageDownloaded != null) ImageDownloaded(bmp);
                 }, true);
            }
            else
            {
                //if it's a RAW image, cancel the download and release the image
                SendSDKCommand(delegate { Error = EDSDK.EdsDownloadCancel(ObjectPointer); });
                Error = EDSDK.EdsRelease(ObjectPointer);
            }
        }

这个项目的所有者为这个错误回答了第二个错误:

DownloadImage(IntPtr)方法不返回图像(它返回void)。但由于该活动希望获得图像,它表示无法将空白转换为图像。您需要从拥有图像对象的DownloadImage方法中激发该事件。

我不知道该怎么办。我试图剪切并只显示我有问题的代码。

整个项目就在这里:

项目

我知道这个问题有一些代码,但我无法进一步解释。

如何使用佳能相机库拍摄照片,并在pictureBox中显示拍摄的图像照片?正在获取错误

为什么不使用"浏览"按钮1来浏览和搜索图像,然后将其显示在图片框上?

        private void Browse_Click(object sender, EventArgs e)
    {
        OpenFileDialog OFD = new OpenFileDialog();
        if (OFD.ShowDialog() == DialogResult.OK)
        {
            Bitmap Image = new Bitmap(OFD.FileName);
            NewUserPictureBox.Image = Image;
            NewUserPictureBox.SizeMode = PictureBoxSizeMode.Zoom;
        }
    }