簡體   English   中英

無法將類型對象(即位圖)轉換為位圖

[英]Can't cast type object (which is a Bitmap) to Bitmap

我有以下代碼:

using System;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using DlibDotNet;
using DlibDotNet.Extensions;
using OpenCvSharp;
using OpenCvSharp.Extensions;

namespace MyNamespace.CompVis.FaceSwap
{
public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
    }

    public MainForm(string title, Bitmap bitmap)
    {
        this.Text = title;
        pictureBox = new PictureBox();
        this.Controls.Add(pictureBox);
        pictureBox.Dock = DockStyle.Fill;
        pictureBox.SizeMode = PictureBoxSizeMode.Zoom;
        pictureBox.Image = bitmap;
    }
    private void MainForm_Load(object sender, EventArgs e)
    {
        // load the input image
        string sPathTemplate = "d:\\facewarp2\\heads\\template.jpg";
        System.Drawing.Bitmap nBmpTemplate = Bitmap.FromFile(sPathTemplate) as Bitmap;

        string sPathIntruder = "d:\\facewarp2\\heads\\swap.jpg";
        System.Drawing.Bitmap nBmpIntruder = Bitmap.FromFile(sPathIntruder) as Bitmap;

        // process image
        System.Drawing.Bitmap newBitmap = ProcessImage(nBmpTemplate, nBmpIntruder);
    }

    private System.Drawing.Bitmap ProcessImage(System.Drawing.Bitmap uTemplateImage, System.Drawing.Bitmap uIntruderImage)
    {
         System.Drawing.Bitmap nClone = uTemplateImage.Clone();

最后一行失敗。 編譯器告訴我

The type "object" can't be implicitely cast to "System.Drawing.Bitmap". An explicit conversion exists (possible a conversion is missing).

我的代碼有什么問題?

Clone方法返回一種對象。

但是,nClone的類型為Bitmap。

為了防止這種情況,您應該使用

Bitmap nClone = (System.Drawing.Bitmap) uTemplateImage.Clone();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM