我的问题是:
以下是我的数据库连接代码(成功代码):
public static void getTableItems(DataTable dt, int bodySectionNo)
{
// gets the oledbconnection object to open and access
var con = GetConnection();
try
{
con.Open(); // opens db connection
// creates the sql query for db items change bodysection = # for different bodySections
OleDbCommand command = new OleDbCommand("SELECT itemNo, itemName, NSN, bodySection, Image.FileName, Image.FileData, Image.FileType FROM tblItems WHERE bodySection = " + bodySectionNo + ";", con);
OleDbDataAdapter oleAdapter = new OleDbDataAdapter(command); // executes the command and retrieves the data from the db
oleAdapter.Fill(dt); // fills the datatable with the query results
}
catch(Exception e)
{
// writes to console any errors for this connection
Console.WriteLine("ERROR: getTableItemsForHead, " + e.Message);
}
finally
{
con.Close(); // closes db connection
}
}
在我的MainWindow.xaml.cs文件中,我试图读取数据表,存储数据,然后将数据传递到元窗口以显示主窗口。 这是我的MainWindow.xaml.cs代码(不成功的代码):
private void populateComboBox(DataTable dt, ComboBox cb)
{
foreach (DataRow row in dt.Rows)
{
WrapPanel wp = new WrapPanel();
TextBlock txtItemName = new TextBlock();
TextBlock txtNSN = new TextBlock();
Image img = new Image();
// creates a textbox and adds it to wrappanel
txtItemName.Text = Convert.ToString(row["itemName"]) + " ";
wp.Children.Add(txtItemName);
// creates a textbox and adds it to wrappanel
txtNSN.Text = Convert.ToString(row["NSN"]) + " ";
wp.Children.Add(txtNSN);
// stores image properties from datatable
var fileName = row["Image.FileName"];
var fileData = (byte[])row["Image.FileData"];
var fileType = row["Image.FileType"];
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.CreateOptions = BitmapCreateOptions.None;
bi.CacheOption = BitmapCacheOption.Default;
bi.StreamSource = new MemoryStream(fileData);
bi.EndInit();
img.Source = bi;
wp.Children.Add(img);
cb.Items.Add(wp);
}
}
我尝试了多种不同的方法将byte []转换为位图,bitmapimage,bitmapsource,image,imagesource等。我找不到解决此问题的片段" No Imaging组件适合完成此操作操作被发现" 它打破了bi.EndInit();线。
System.NotSupportedException未被用户代码
处理 HResult = -2146233067消息=没有适合的成像组件 完成此操作被发现。来源= PresentationCore
堆栈跟踪: 在System.Windows.Media.Imaging.BitmapDecoder.SetupDecoderFromUriOrStream(Uri) uri,Stream stream,BitmapCacheOption cacheOption,Guid& CLSID, 布尔和放大器; isOriginalWritable,Stream& uriStream,UnmanagedMemoryStream& unmanagedMemoryStream,SafeFileHandle& safeFilehandle) 在System.Windows.Media.Imaging.BitmapDecoder.CreateFromUriOrStream(Uri) baseUri,Uri uri,Stream stream,BitmapCreateOptions createOptions, BitmapCacheOption cacheOption,RequestCachePolicy uriCachePolicy, Boolean insertInDecoderCache) 在System.Windows.Media.Imaging.BitmapImage.FinalizeCreation() 在System.Windows.Media.Imaging.BitmapImage.EndInit() 位于P:\所有Documents \ BBMD 18 \ CWEDA EH \ CWEDA Design 1 \ CWEDA中的CWEDA_take_1.SecondWindow.populateComboBox(DataTable dt,ComboBox cb) 取1 \ SecondWindow.xaml.cs:第286行 在CWEDA_take_1.SecondWindow..ctor()在P:\所有Documents \ BBMD 18 \ CWEDA EH \ CWEDA设计1 \ CWEDA取1 \ SecondWindow.xaml.cs:第36行
的InnerException: 错误码= -2003292336 的HResult = -2003292336 消息=无法找到组件。 (来自HRESULT的异常:0x88982F50) 的InnerException:
答案 0 :(得分:0)
此页面中的代码与您的代码略有不同:
http://csharphelper.com/blog/2015/07/display-images-in-an-access-database-in-wpf-and-c/
如果此方法有效并且链接在将来中断。代码确实:
if (reader.IsDBNull(6))
imgCover.Source = null;
else
imgCover.Source =
BytesToImage((byte[])reader.GetValue(6));
那个方法:
// Convert a byte array into a BitmapImage.
private static BitmapImage BytesToImage(byte[] bytes)
{
var bm = new BitmapImage();
using (MemoryStream stream = new MemoryStream(bytes))
{
stream.Position = 0;
bm.BeginInit();
bm.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
bm.CacheOption = BitmapCacheOption.OnLoad;
bm.UriSource = null;
bm.StreamSource = stream;
bm.EndInit();
}
return bm;
}