![](/img/trans.png)
[英]cannot implicitly convert type system.drawing.image to system.windows.controls.image
[英]Cannot implicitly convert type 'System.Drawing.Image' to 'System.Drawing.Bitmap'`
聲明了一個位圖
private Bitmap img1 = null;
private Bitmap img2 = null;
從openFileDialog 中選擇圖像后,圖像將被放置。
選定的圖像被放置在一個數組中。
imgName = openFD.FileNames;
然后 button1 顯示這些圖像。
pictureBox1.Image = Image.FromFile(imgName[0]);
pictureBox2.Image = Image.FromFile(imgName[1]);
我用這個替換了 button1 代碼
img1 = Image.FromFile(imgName[0]);
img2 = Image.FromFile(imgName[1]);
但發生錯誤
無法將類型“System.Drawing.Image”隱式轉換為“System.Drawing.Bitmap”
我會嘗試將代碼更改為img1 = Bitmap.FromFile(imgName[0]);
. 但仍然有同樣的錯誤。
任何建議如何糾正或正確執行此操作?
img1 = new Bitmap(imgName[0]);
img2 = new Bitmap(imgName[1]);
img1 = (Bitmap) Image.FromFile(imgName[0]);
img2 = (Bitmap) Image.FromFile(imgName[1]);
由於錯誤消息說您不能隱式執行此操作,因此您需要將其顯式轉換為Bitmap
編輯
根據下面的評論,我建議要么使用 icktoofay 的答案,即使用 Bitmap 構造函數,或者如果您可以直接使用 Image 類而不是使用 Bitmap,您也可以使用它
我在我的代碼中使用了這三行來將字節值分配給 c# 中的圖像。
ImageConverter converter = new ImageConverter();
pbStudentPicture.Image = (Image)converter.ConvertFrom(StudentPinfo.Picture);
pbStudentPicture.SizeMode = PictureBoxSizeMode.StretchImage;
就我而言,我的System.Drawing.Image
不是來自文件,而是來自設備的相機。 由於我不想將圖像保存到文件作為中間步驟,因此我使用了以下方法:
var imageBitmap = new Bitmap(image.Width, image.Height);
using (var graphics = Graphics.FromImage(imageBitmap))
graphics.DrawImage(image, 0, 0, image.Width, image.Height);
也許這對其他人有幫助;)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.