Недопустимая операция при gcnew Image CLI / переполнение стека

Я получаю странную информацию о invalidOperationException в PresentationCore.dll при создании изображения gcnew Image(),
Я присоединяю проект и файл JPG (вставьте его, пожалуйста, в C :). На самом деле это невозможно проверить другим способом, потому что настройка проекта (ссылок) заняла много времени, и просто скопированный код не будет работать.

http://www.speedyshare.com/Vrr84/Jpg.zip

Пожалуйста, помогите мне решить эту проблему.

введите описание изображения здесь

 // 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;
}

0

Решение

Основная проблема здесь:

Вызывающий поток должен быть STA […]

Ваш основной поток должен быть помечен как однопоточная квартира (Для краткости STA) для правильной работы WPF. Исправление? добавлять [System::STAThread] на ваш _tmain, таким образом, информируя среду выполнения о том, что главной ролью должна быть STA.

[System::STAThread]
int _tmain(int argc, _TCHAR* argv[])
{
// the rest of your code doesn't change
}
2

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector