1

我已经通过 CLI/C++ 加载了一个图像,我想在表单中显示它(运行程序时打开了 Form1,如何把它放在那里)。我已经评论了我想在表格中输入的图像

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

3 回答 3

0

.NET 图形类 (GDI+) 非常适合绘制图形。您不仅可以使用它在画布上绘制图像,还可以绘制许多其他内容。只需在 Google 上搜索“GDI+ 教程”或“.NET 图形”之类的内容,您就会找到一些东西。

此外,您加载该 JPEG 的方式,在 .NET 中有更简单的方法可以做到这一点

于 2012-11-22T00:41:03.557 回答
0

据我了解,您想实时显示绘图。对于任何只想在表单中显示图像的人。针对 VS19 测试的代码。您可以通过以下方式在图像框中打开图像:

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

如果您必须事先格式化图像,只需先将其保存,然后打开保存的文件:

System::Drawing::Image^ myImage;
//do smth
myImage->Save(L"C:/heartBUFF.jpg");
pictureBox1->Image = System::Drawing::Image::FromFile(L"C:/heartBUFF.jpg");
于 2020-07-23T16:11:23.380 回答
0

使用 .net 4.7.2 进行简短的部分回复。

我不了解 System::Windows::Media::Imaging,它似乎根本不起作用或根本不存在。

请改用 System::Drawing::Imaging。我想简单地做 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);

希望它有所帮助,如果仍然需要,当然。有一个问题是质量参数(此处为 95)必须是 long long,long 不是一个选项,而其他可能性(unsigned char 和 short)则不起作用。

于 2021-09-05T13:31:57.617 回答