将保存位图图像复制到文件C ++ [英] Copy save bitmap image to file C++

查看:124
本文介绍了将保存位图图像复制到文件C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我试图复制并将Bitmap类图像的内容保存到* .bmp文件,但当前在控制台程序的运行时期间接收未处理的异常。我将很高兴知道错误所在以及如何修复此程序。谢谢。



以下是主程序代码的主要部分:



Hello. I am trying to copy and save the contents of a Bitmap class image to a *.bmp file but currently receive Unhandled Exceptions during run-time of the console program. I would be grateful to learn where the error lies and how to fix this program. Thank you.

Below is the main part of the code in the Main program:

Bitmap^ imageTest;
String^ path = "C:\\ImageScan.bmp";
imageTest = gcnew Bitmap(path, true); // Actual image to be copied.

Bitmap^ imageTestCopy;
String^ path2 = "C:\\BrainScanCopy2.bmp"; //'blank canvas' identical to imageTest
imageTestCopy = gcnew Bitmap(path2, true);


int x = 0;
int y = 0;

for (x = 0; x < imageTest->Width; x++) // Loop through the image pixels
{
	for (y = 0; y < imageTest->Height; y++)
	{
	        Color pixelColor = imageTest->GetPixel(x, y);
		imageTestCopy->SetPixel(x, y, pixelColor);
	}
}

imageTestCopy->Save("c:\\BrainScanCopy3.bmp"); //save image to new file





我尝试了什么:



我试过更改路径并使用imageTestCopy2.bmp代替尝试将图像保存到文件时imageTestCopy3.bmp。



What I have tried:

I have tried changing the path and using "imageTestCopy2.bmp" instead of "imageTestCopy3.bmp" when trying to save the image to a file.

推荐答案

您不尊重bmp文件的文件格式。在开头写的是位图头,比可选的一些调色板(映射)数据和像素。您可以轻松地在维基百科关于bmp文件格式的文章中获取一些信息。



传奇文章 CxImage 以优异的品质处理位图和其他一些图形格式。
You dont respect the file format of bmp files. At the beginning is written the bitmap header, than optional some palette (mapping) data and than the pixels. You easily can fetch some information in the wikipedia article about bmp file format.

The legendary article CxImage handles the bitmap and some other graphic formats in an outstanding quality.


这个通用异常是处理图形对象时可以得到的最严重的异常。它只是根本没有提供任何东西。

This generic exception is the worst exception that one can get while working with graphics objects. It just simply doesn't provide anything at all.
Quote:

System.Drawing中发生了一个未处理的System.Runtime.InteropServices.ExternalException类型异常。 DLL附加信息:GDI +中发生了一般性错误。

An unhandled exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll Additional information: A generic error occurred in GDI+.



即使你试图获得 ErrorCode 它也不会提供任何内容一点都不其次,要理解的主要是,一旦加载图形(位图),就会在该文件上放置一个锁,该锁不允许更新或更改文件。这个 Hans Passant解决方案 [ ^ ]概述了使用using块以确保先前的位图正确处理;你可以写一个 try ... catch 替代那个C#程序来在你的C ++应用程序中执行它。



更多信息,请 ExternalException Class(System.Runtime。 InteropServices) [ ^ ]


Even if you tried to get the ErrorCode it won't provide anything at all. Secondly, the main thing to understand is that once you load the graphics (bitmap), a lock is put on that file which doesn't allow updating or altering the file. This solution of Hans Passant[^] gives a good overview of using the using block to ensure that the previous bitmaps are properly disposed; you can write a try...catch alternative of that C# program to perform this in your C++ application.

For more, ExternalException Class (System.Runtime.InteropServices)[^]


这篇关于将保存位图图像复制到文件C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆