- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在尝试弄清楚如何在 CArchive (JPEG) 中正确存储 CImage 文件。我目前的做法是以下(伪)代码:
BOOL CPicture::Serialize(CArchive &ar)
{
IStream *pStream = NULL;
HRESULT hr;
CImage *img = GetImage();
if (ar.IsLoading())
{
HGLOBAL hMem = GlobalAlloc(GMEM_FIXED, 54262);
hr = CreateStreamOnHGlobal(hMem, FALSE, &pStream);
if(SUCCEEDED(hr))
{
ar.Read(pStream, 54262);
img->Load(pStream);
pStream->Release();
GlobalUnlock(hMem);
GlobalFree(hMem);
}
}
else
{
hr = CreateStreamOnHGlobal(0, TRUE, &pStream);
if (SUCCEEDED(hr))
{
hr = img->Save(pStream, Gdiplus::ImageFormatJPEG);
if (SUCCEEDED(hr))
ar.Write(pStream, 54262);
}
}
...
我刚刚回到 C++,过去只用过一点点。任何帮助将不胜感激。
非常感谢您。
最佳答案
我不是 IStream
方面的专家,但我认为您可能没有正确使用它。以下代码似乎有效。它目前以 png 格式存档,但可以通过 Gdiplus::ImageFormatPNG
轻松更改。这在很大程度上要归功于 S. Arman 在 CodeProject 上的教程“将 IStream
视为字节流”。 .
void ImageArchive(CImage *pImage, CArchive &ar)
{
HRESULT hr;
if (ar.IsStoring())
{
// create a stream
IStream *pStream = SHCreateMemStream(NULL, 0);
ASSERT(pStream != NULL);
if (pStream == NULL)
return;
// write the image to a stream rather than file (the stream in this case is just a chunk of memory automatically allocated by the stream itself)
pImage->Save(pStream, Gdiplus::ImageFormatPNG); // Note: IStream will automatically grow up as necessary.
// find the size of memory written (i.e. the image file size)
STATSTG statsg;
hr = pStream->Stat(&statsg, STATFLAG_NONAME);
ASSERT(hr == S_OK);
ASSERT(statsg.cbSize.QuadPart < ULONG_MAX);
ULONG nImgBytes = ULONG(statsg.cbSize.QuadPart); // any image that can be displayed had better not have more than MAX_ULONG bytes
// go to the start of the stream
LARGE_INTEGER seekPos;
seekPos.QuadPart = 0;
hr = pStream->Seek(seekPos, STREAM_SEEK_SET, NULL);
ASSERT(hr == S_OK);
// get data in stream into a standard byte array
char *bytes = new char[nImgBytes];
ULONG nRead;
hr = pStream->Read(bytes, nImgBytes, &nRead); // read the data from the stream into normal memory. nRead should be equal to statsg.cbSize.QuadPart.
ASSERT(hr == S_OK);
ASSERT(nImgBytes == nRead);
// write data to archive and finish
ar << nRead; // need to save the amount of memory needed for the file, since we will need to read this amount later
ar.Write(bytes, nRead); // write the data to the archive file from the stream memory
pStream->Release();
delete[] bytes;
}
else
{
// get the data from the archive
ULONG nBytes;
ar >> nBytes;
char *bytes = new char[nBytes]; // ordinary memory to hold data from archive file
UINT nBytesRead = ar.Read(bytes, nBytes); // read the data from the archive file
ASSERT(nBytesRead == UINT(nBytes));
// make the stream
IStream *pStream = SHCreateMemStream(NULL, 0);
ASSERT(pStream != NULL);
if (pStream == NULL)
return;
// put the archive data into the stream
ULONG nBytesWritten;
pStream->Write(bytes, nBytes, &nBytesWritten);
ASSERT(nBytes == nBytesWritten);
if (nBytes != nBytesWritten)
return;
// go to the start of the stream
LARGE_INTEGER seekPos;
seekPos.QuadPart = 0;
hr = pStream->Seek(seekPos, STREAM_SEEK_SET, NULL);
ASSERT(hr == S_OK);
// load the stream into CImage and finish
pImage->Load(pStream); // pass the archive data to CImage
pStream->Release();
delete[] bytes;
}
}
关于c++ - 序列化 : CArchive a CImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41506404/
我想将对象列表序列化和反序列化到一个文件中。但是我发现如果我使用CArchive,新的内容会覆盖文件中原来的内容,这不是我所期望的。有什么方法可以让我从文件中的指定位置读取和写入?谢谢! 最佳答案 C
我有一段代码,其中使用 CArchive 进行读写和归档。经过调查,我发现 CArchive 对象在从文件的不同部分读取数据时会更改其位置。例如,如果文件结构是先有页眉,然后是正文,然后是页脚。现在,
我目前正在尝试弄清楚如何在 CArchive (JPEG) 中正确存储 CImage 文件。我目前的做法是以下(伪)代码: BOOL CPicture::Serialize(CArchive &ar)
我在应用程序中有几个设置,我希望在更改时保存。有复选框: void CL2HamsterDlg::OnBnClickedCheckAutoselect() { CFile theFile( p
我通过 Visual Studio 2010 学习 Visual C++。我尝试使用 MFC CObject 的序列化函数。我无法使用序列化函数加载我的对象我的代码: #include #inclu
我正在使用 MFC 的 CArhive 类来保存我的应用程序的项目文件。其中一个类名非常不准确,我想更改它,但是简单地在各处更改名称会使以前的存档文件无用并出现存档错误。有没有一种方法可以更改存档类的
我正在尝试用 C# 读入一个用 CArchive 编写的文件。据我所知,格式是: [下一组数据的长度][数据]...等 不过,我对某些数据仍然很模糊。如何读取日期数据? float 、整数、 doub
这听起来可能很简单,但是: WORD wSong = 0; CArchive ar; ... ar >> wSong; m_sPublicTalkInfo.iSongStart = static_ca
我在我的 MFC 应用程序中使用 std::string,我想将它存储在文档的 Serialize() 函数中。我不想将它们存储为 CString,因为它在那里写了自己的东西,我的目标是创建一个我知道
我需要把一个map的内容(key是int的ID,value是自定义的struct)写入一个文件,稍后从文件中加载。我可以在带有 CArchive 的 MFC 中完成吗? 谢谢! 最佳答案 在 MFC
我正在比较 MFC 类之间的性能: CFile + CArchive(使用缓冲区) 和 CStdioFile(使用缓冲 I/O 流) 两者都使用缓冲区。 以下是我的代码: void TestFun1(
我在 VS2010 VC++ 中序列化和反序列化指针数据时遇到问题。基本上,该程序无法序列化和反序列化 CObList * 指针数据。源代码执行正常步骤来处理该过程: 1) MyObject继承自CO
我有一个程序,可以绘制形状:菱形、正方形、矩形、直线、圆形……它与 Microsoft 中的 Paint 相同。我的问题是使用 Serialize(CArchive &) 保存和加载文件,但在使用 C
是否有关于用于序列化各种 MFC 数据结构的二进制格式的明确文档?我已经能够在十六进制编辑器中查看我自己的一些类,并使用 Java 的 ByteBuffer 类读取它们(使用自动字节顺序转换等)。 但
我是一名优秀的程序员,十分优秀!