简体   繁体   English

如何将加载的图像以 CLI/C++ 的形式放置

[英]How to put loaded image in the form CLI/C++

I have loaded an Image by CLI/C++ and i would like to display it in the form(there is Form1 turning on when running program, how to put it there).我已经通过 CLI/C++ 加载了一个图像,我想在表单中显示它(运行程序时打开了 Form1,如何把它放在那里)。 I have commented which image I would like to put in the form我已经评论了我想放入表格中的图片

// a.cpp : main project file. // a.cpp : 主项目文件。

#include "stdafx.h"
#include "Form1.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;
using namespace a;

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
    // Enabling Windows XP visual effects before any controls are created
    Application::EnableVisualStyles();
    Application::SetCompatibleTextRenderingDefault(false); 

    // Create the main window and run it
    Application::Run(gcnew Form1());


    // 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
        System::Windows::Controls::Image^ myImage = gcnew System::Windows::Controls::Image();  //<--- this image in the Form1  -------
        myImage->Source = bitmapSource;
        myImage->Stretch = Stretch::None;
        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 .NET Graphics class (GDI+) is great for, well, drawing graphics. .NET 图形类 (GDI+) 非常适合绘制图形。 You can use it to draw not just images to a canvas, but many other things.您不仅可以使用它将图像绘制到画布上,还可以绘制许多其他东西。 Just google something like 'GDI+ tutorials' or '.NET Graphics', and you'll find something.只需在谷歌上搜索诸如“GDI+ 教程”或“.NET 图形”之类的内容,您就会找到一些东西。

Also, the way you loaded that JPEG, there are much easier ways to do that in .NET此外,您加载 JPEG 的方式,在 .NET 中有更简单的方法来做到这一点

As I understand you want to show drawing in real time.据我了解,您想实时显示绘图。 For anybody that just wants to show an image in the form.对于任何只想在表单中显示图像的人。 Code tested for VS19.针对 VS19 测试的代码。 You can open an image in an image box through :您可以通过以下方式在图像框中打开图像:

pictureBox1->Image = System::Drawing::Image::FromFile(L"C:/heart.jpg");

If you have to format the image beforehand, just save it first and then open the saved file:如果您必须事先格式化图像,只需先保存它,然后打开保存的文件:

System::Drawing::Image^ myImage;
//do smth
myImage->Save(L"C:/heartBUFF.jpg");
pictureBox1->Image = System::Drawing::Image::FromFile(L"C:/heartBUFF.jpg");

A short and therefore, partial reply, using .net 4.7.2.使用 .net 4.7.2 进行简短的部分回复。

I don't know about System::Windows::Media::Imaging, it just did not seem to work or be there at all.我不知道 System::Windows::Media::Imaging,它似乎根本不起作用或根本不存在。

Use System::Drawing::Imaging instead.请改用 System::Drawing::Imaging。 I wanted to simply do jpeg quality and ended up with the following:我只想简单地做 jpeg 质量并最终得到以下结果:

cli::array<Imaging::ImageCodecInfo^>^ imcoi = Imaging::ImageCodecInfo::GetImageEncoders(); int j;
for (j = 0; j < imcoi->Length; ++j)
{ if (imcoi[j]->MimeType == "image/jpeg") break; }
assert(j < imcoi->Length);
Imaging::ImageCodecInfo^ myImCodecInf = imcoi[j];
Imaging::Encoder^ myenc = Imaging::Encoder::Quality;
Imaging::EncoderParameters^ myencparams = gcnew Imaging::EncoderParameters(1);
Imaging::EncoderParameter^ myencparam = gcnew Imaging::EncoderParameter(myenc,95LL);
myencparams->Param[0] = myencparam;
img->Save(refstring(fn), myImCodecInf, myencparams);

hope it helps, if still needed, of course.希望它有帮助,如果仍然需要,当然。 There is a gotcha in that the quality param (here 95) must be long long, simply long is not an option and the other possibilities (unsigned char and short) just do not work.有一个问题是质量参数(这里是 95)必须是 long long,只是 long 不是一个选项,其他可能性(unsigned char 和 short)不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM