c# Clipboard.SetDataObject(bmp1) 在可以调用 OLE 之前,必须将当前线程设置为单线程单元(STA)模式。请确保您的 Main 函数带有 STAThreadAttribute 标记。 只有将调试器附加到该进程才会引发此异常

时间:2023-03-08 19:50:06

c# Clipboard.SetDataObject(bmp1)  在可以调用 OLE 之前,必须将当前线程设置为单线程单元(STA)模式。请确保您的 Main 函数带有 STAThreadAttribute 标记。 只有将调试器附加到该进程才会引发此异常

try
{
//Image bmp1 = Image.FromFile("C:\\Users\\jake\\Desktop\\03.jpeg");
// Clipboard.Clear(); Clipboard.SetDataObject(image); DataFormats.Format dataFormat1 = DataFormats.GetFormat(DataFormats.Bitmap);
if (richShow.CanPaste(dataFormat1))
{
richShow.Paste(dataFormat1);
} }
catch (Exception exc)
{
MessageBox.Show("图片插入失败。" + exc.Message, "提示",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}

改为

Thread th = new Thread(new ThreadStart(delegate ()
{
try
{
//Image bmp1 = Image.FromFile("C:\\Users\\jake\\Desktop\\03.jpeg");
// Clipboard.Clear(); Clipboard.SetDataObject(image); DataFormats.Format dataFormat1 = DataFormats.GetFormat(DataFormats.Bitmap);
if (richShow.CanPaste(dataFormat1))
{
richShow.Paste(dataFormat1);
} }
catch (Exception exc)
{
MessageBox.Show("图片插入失败。" + exc.Message, "提示",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}));
th.TrySetApartmentState(ApartmentState.STA);
th.Start();
th.Join();