0

で画像を作成しているときinvalidOperationExceptionに、に関する奇妙な情報を取得します。PresentationCore.dllgcnew 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;
}
4

1 に答える 1

2

中心的な問題はそこにあります:

呼び出し元のスレッドはSTAである必要があります[...]

WPFが正しく機能するには、メインスレッドをシングルスレッドアパートメント(略してSTA)としてマークする必要があります。修正?に追加[System::STAThread]して_tmain、メインのテーマがSTAである必要があることをランタイムに通知します。

[System::STAThread]
int _tmain(int argc, _TCHAR* argv[])
{
    // the rest of your code doesn't change
}
于 2012-11-22T00:36:26.523 に答える