简体   繁体   English

gcnew Image CLI / C ++时无效操作

[英]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.

相关问题 在没有gcnew的情况下创建C ++ / CLI对象 - Creating C++/CLI objects without gcnew GLSL C ++ glVertexAttribPointer和glDrawElements返回GL_INVALID_OPERATION - GLSL C++ glVertexAttribPointer & glDrawElements return GL_INVALID_OPERATION C ++:在混合使用C ++ / CLI和C ++时获取c3859错误代码 - C++: Getting the c3859 error code while compiling in a mix of C++/CLI and C++ C ++图像处理:均匀的平滑操作使图像更暗 - C++ Image Processing: Uniform Smoothing Operation Makes Image Darker 包装C ++共享库时在C ++ / CLI项目中链接错误 - Linking error while in C++/CLI project while wrapping C++ shared library 使用C ++ / CLI包装器DLL时缺少功能 - Missing Function while using C++/CLI wrapper dll 在托管代码中使用非托管代码时处理错误(C++、C、C++/CLI、C#) - Handling errors while using unmanaged code in a managed one ( C++, C, C++/CLI, C#) 全屏显示并在C ++ / CLI中重新绘制图像 - Repainting image after going to full screen and back in C++/CLI 在 C++ 中执行字符串复制操作时出错 - Error while performing string copy operation in C++ 在理解移动语义的同时,c ++程序中的操作混乱 - Confusing operation in c++ program while understanding move semantics
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM