CImage 写下像素但保存的图片确是这个样子
CImage 写入像素但保存的图片确是这个样子
明明写入像素值了但打开的图片却是:
就好像是张白图什么都没有?
特来请教各位前辈?
------解决思路----------------------
------解决思路----------------------
CImage::Save
Saves an image to the specified stream or file on disk.
HRESULT Save(
IStream* pStream,
REFGUID guidFileType
) const throw();
HRESULT Save(
LPCTSTR pszFileName,
REFGUID guidFileType= GUID_NULL
) const throw();
Parameters
pStream
A pointer to a COM IStream object containing the file image data.
pszFileName
A pointer to the file name for the image.
guidFileType
The file type to save the image as. Can be one of the following:
ImageFormatBMP An uncompressed bitmap image.
ImageFormatPNG A Portable Network Graphic (PNG) compressed image.
ImageFormatJPEG A JPEG compressed image.
ImageFormatGIF A GIF compressed image.
NoteNote
For a complete list of constants, see Image File Format Constants in the Platform SDK.
Return Value
A standard HRESULT.
Remarks
Call this function to save the image using a specified name and type. If the guidFileType parameter is not included, the file name's file extension will be used to determine the image format. If no extension is provided, the image will be saved in BMP format.
Example:
// Demonstrating saving various file formats
int _tmain(int argc, _TCHAR* argv[])
{
CImage myimage;
// load existing image
myimage.Load("image.bmp");
// save an image in BMP format
myimage.Save("c:\image1.bmp");
// save an image in BMP format
myimage.Save("c:\image2",ImageFormatBMP);
// save an image in JPEG format
myimage.Save("c:\image3.jpg");
// save an image in BMP format, even though jpg file extension is used
myimage.Save("c:\image4.jpg",ImageFormatBMP);
return 0;
}
COLORREF pixel;
BYTE r, g, b, avg;
CImage image;
image.Load("src.png");
if(image.IsNull())
{
}
int w = image.GetWidth();
int h = image.GetHeight();
for(int x = 0; x < w; ++ x)
for(int y = 0; y < h; ++ y)
{
pixel = image.GetPixel(x, y);
r = GetRValue(pixel);
g = GetGValue(pixel);
b = GetBValue(pixel);
avg = ((r+g+b)/3);
image.SetPixel(x, y, avg);
//image.SetPixelRGB(x, y, avg, avg, avg);
}
image.Save(("mysrc1.png"));
明明写入像素值了但打开的图片却是:
就好像是张白图什么都没有?
特来请教各位前辈?
------解决思路----------------------
COLORREF pixel;
BYTE r, g, b, avg;
CImage image;
image.Load("src.png");
if(image.IsNull())
{
OutputDebugString("文件不能存在!");
return ;
}
int w = image.GetWidth();
int h = image.GetHeight();
for(int x = 0; x < w; ++ x)
for(int y = 0; y < h; ++ y)
{
pixel = image.GetPixel(x, y);
r = GetRValue(pixel);
g = GetGValue(pixel);
b = GetBValue(pixel);
avg = ((r+g+b)/3);
image.SetPixel(x, y, avg);
//image.SetPixelRGB(x, y, avg, avg, avg);
}
if(!image)
{
OutputDebugString("操作成功!");
if(TRUE == image.Save(("mysrc1.png")))
{
OutputDebugString("保存成功!");
}
}
------解决思路----------------------
CImage::Save
Saves an image to the specified stream or file on disk.
HRESULT Save(
IStream* pStream,
REFGUID guidFileType
) const throw();
HRESULT Save(
LPCTSTR pszFileName,
REFGUID guidFileType= GUID_NULL
) const throw();
Parameters
pStream
A pointer to a COM IStream object containing the file image data.
pszFileName
A pointer to the file name for the image.
guidFileType
The file type to save the image as. Can be one of the following:
ImageFormatBMP An uncompressed bitmap image.
ImageFormatPNG A Portable Network Graphic (PNG) compressed image.
ImageFormatJPEG A JPEG compressed image.
ImageFormatGIF A GIF compressed image.
NoteNote
For a complete list of constants, see Image File Format Constants in the Platform SDK.
Return Value
A standard HRESULT.
Remarks
Call this function to save the image using a specified name and type. If the guidFileType parameter is not included, the file name's file extension will be used to determine the image format. If no extension is provided, the image will be saved in BMP format.
Example:
// Demonstrating saving various file formats
int _tmain(int argc, _TCHAR* argv[])
{
CImage myimage;
// load existing image
myimage.Load("image.bmp");
// save an image in BMP format
myimage.Save("c:\image1.bmp");
// save an image in BMP format
myimage.Save("c:\image2",ImageFormatBMP);
// save an image in JPEG format
myimage.Save("c:\image3.jpg");
// save an image in BMP format, even though jpg file extension is used
myimage.Save("c:\image4.jpg",ImageFormatBMP);
return 0;
}