[英]Invalid operation while gcnew Image CLI/C++
I get weird information about invalidOperationException
in PresentationCore.dll
while constructing an image by gcnew Image()
. 我在使用
gcnew Image()
构建图像时,会在PresentationCore.dll
获得有关invalidOperationException
奇怪信息。 I attach project and the JPG fle (put it please in in C:) It actually cannot be checked other way cause configuration of project(references) took a long time, and just copied code will not work. 我附加项目和JPG文件(请把它放在C :)它实际上无法检查其他方式导致项目(引用)的配置花了很长时间,只是复制代码将无法正常工作。
http://www.speedyshare.com/Vrr84/Jpg.zip http://www.speedyshare.com/Vrr84/Jpg.zip
Please help me solve that problem. 请帮我解决这个问题。
// Jpg.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#using <mscorlib.dll> //requires CLI
using namespace System;
using namespace System::IO;
using namespace System::Windows::Media::Imaging;
using namespace System::Windows::Media;
using namespace System::Windows::Controls;
int _tmain(int argc, _TCHAR* argv[])
{
// Open a Stream and decode a JPEG image
Stream^ imageStreamSource = gcnew FileStream("C:/heart.jpg", FileMode::Open, FileAccess::Read, FileShare::Read);
JpegBitmapDecoder^ decoder = gcnew JpegBitmapDecoder(imageStreamSource, BitmapCreateOptions::PreservePixelFormat, BitmapCacheOption::Default);
BitmapSource^ bitmapSource = decoder->Frames[0];//< --mamy bitmape
// Draw the Image
Image^ myImage = gcnew Image();//<----------- ERROR
myImage->Source = bitmapSource;
myImage->Stretch = Stretch::None;
myImage->Margin = System::Windows::Thickness(20);
//
int width = 128;
int height = width;
int stride = width / 8;
array<System::Byte>^ pixels = gcnew array<System::Byte>(height * stride);
// Define the image paletteo
BitmapPalette^ myPalette = BitmapPalettes::Halftone256;
// Creates a new empty image with the pre-defined palette.
BitmapSource^ image = BitmapSource::Create(
width, height,
96, 96,
PixelFormats::Indexed1,
myPalette,
pixels,
stride);
System::IO::FileStream^ stream = gcnew System::IO::FileStream("new.jpg", FileMode::Create);
JpegBitmapEncoder^ encoder = gcnew JpegBitmapEncoder();
TextBlock^ myTextBlock = gcnew System::Windows::Controls::TextBlock();
myTextBlock->Text = "Codec Author is: " + encoder->CodecInfo->Author->ToString();
encoder->FlipHorizontal = true;
encoder->FlipVertical = false;
encoder->QualityLevel = 30;
encoder->Rotation = Rotation::Rotate90;
encoder->Frames->Add(BitmapFrame::Create(image));
encoder->Save(stream);
return 0;
}
The core issue is there: 核心问题是:
The calling thread must be STA [...]
调用线程必须是STA [...]
Your main thread must be marked as a single-threaded apartment (STA for short) for WPF to function correctly. 您的主线程必须标记为单线程单元 (简称STA)才能使WPF正常运行。 The fix?
修复? Add
[System::STAThread]
to your _tmain
, thus informing the runtime that the main thead has to be STA. 将
[System::STAThread]
添加到您的_tmain
,从而通知运行时主要的thead必须是STA。
[System::STAThread]
int _tmain(int argc, _TCHAR* argv[])
{
// the rest of your code doesn't change
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.