这段代码应该有效,但事实并非如此 [英] This code should work but it doesn't

查看:87
本文介绍了这段代码应该有效,但事实并非如此的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的示例代码演示如何使用CImage(来自ATLImage.h)加载基于文件的图像,将其保存到流中,并创建一个COM图片界面,该界面可以是OLE自动化属性中的返回值。



疯狂的是,OleLoadPicture失败并显示错误代码,表明文件格式已损坏。但是,我能够在错误条件下演示流可以再次加载到第二个CImage中并保存回磁盘。两个基于文件的位图的比较显示它们是相同的。



这是一个令人困惑的问题,因为这似乎是在COM中传递图像数据的最佳方式。 />


有谁能想到为什么会失败?我唯一的怀疑是,保存的BMP不符合某些标准,但我可以再次使用任何图像浏览器查看它



This sample code below demonstrates using CImage (from ATLImage.h) to load a file based image, save it to a stream, and create a COM picture interface which can be the return value in an OLE automation property.

The crazy thing is that OleLoadPicture fails with an error code indicating a corrupted file format. However, I am able to demonstrate in the error condition that the stream can again be loaded in a second CImage and saved back to disk. Comparison of the two file based bitmaps shows them to be identical.

It is a confounding problem as this seems the best way to pass image data around in COM.

Can anyone think of why this fails? My only suspicion is that the saved BMP somehow does not conform to some standard but then again I can look at it with any image viewer

///////////////////////////////////////////////////////////////
// OLE Stream file necessary for OleLoadPicture support
COleStreamFile ImageStream;

// Prepare the Image for memory stream serialization
if (ImageStream.CreateMemoryStream())
{
	// GDI+ Image support
	CImage ImageDIB;

	// Attach the DIB to the Image
	if (SUCCEEDED(ImageDIB.Load(L"C:\\Users\\abantly\\AppData\\Local\\Temp\\Image.bmp")))
	{
		// Serialize to the memory stream the DIB to the BMP format
		IStream * pImageStream = ImageStream.GetStream();
		if (pImageStream && SUCCEEDED(ImageDIB.Save(pImageStream,Gdiplus::ImageFormatBMP)))
		{
			CComPtr<IPicture> Picture;
			HRESULT hr = OleLoadPicture(pImageStream,(LONG)ImageStream.GetLength(),FALSE,IID_IPicture,(LPVOID *)&Picture);
			if (SUCCEEDED(hr))
			{
				// FAILS TO GET HERE with code -2146827807
				CComQIPtr<IPictureDisp> PictureDisp(Picture);
			}
			else
			{
				CImage ImageDIB2;
				if (SUCCEEDED(ImageDIB2.Load(pImageStream)))
					ImageDIB2.Save(L"C:\\Users\\abantly\\AppData\\Local\\Temp\\Image2.bmp");
			}

			// Release the stream
			pImageStream->Release();
		}
	}
}

推荐答案

我终于解决了这个问题(很久以前)和我现在正在更新这个问题。问题是流需要在OleLoadPicture之前重绕。我添加以修复它并使其工作的代码是:



I finally did solve the problem (long ago) and am now updating the question. The issue was that the stream needs to be rewound before OleLoadPicture. The code I added to fix it and make it work was:

 // Rewind the stream for picture loading
LARGE_INTEGER li;
li.QuadPart  = 0;
pImageStream->Seek(li,STREAM_SEEK_SET,NULL);


这篇关于这段代码应该有效,但事实并非如此的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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