C++ 如何在C++;在Windows8上?

C++ 如何在C++;在Windows8上?,c++,windows-8,png,jpeg,C++,Windows 8,Png,Jpeg,我一直在研究位图解码器和位图编码器,但我不知道如何将数据从jpg解码器获取到png编码器。我发现的唯一相似之处是BitmapEncoder::CreatefortTranscodingAsync(),但这适用于两种图像格式相同的情况。使用ImageMagick库如何 有一个C++ API…我在2分钟的搜索中找到了这个: #include using namespace std; using namespace Magick; int main(int argc,char **argv)

我一直在研究
位图解码器
位图编码器
,但我不知道如何将数据从jpg解码器获取到png编码器。我发现的唯一相似之处是
BitmapEncoder::CreatefortTranscodingAsync()
,但这适用于两种图像格式相同的情况。

使用ImageMagick库如何

有一个C++ API…我在2分钟的搜索中找到了这个:

#include  
using namespace std; 
using namespace Magick; 
int main(int argc,char **argv) 
{ 
  InitializeMagick(*argv);

  // Read GIF file from disk 
  Image image( "giraffe.gif" );
  // Write to BLOB in JPEG format 
  Blob blob; 
  image.magick( "JPEG" ) // Set JPEG output format 
  image.write( &blob );

  [ Use BLOB data (in JPEG format) here ]

  return 0; 
}
就个人而言,我可以推荐使用ImageMagick的这个库(尽管使用了C API…)


编辑:您可以将图像写入内存块并将字节传递给编码器…

使用ImageMagick库如何

有一个C++ API…我在2分钟的搜索中找到了这个:

#include  
using namespace std; 
using namespace Magick; 
int main(int argc,char **argv) 
{ 
  InitializeMagick(*argv);

  // Read GIF file from disk 
  Image image( "giraffe.gif" );
  // Write to BLOB in JPEG format 
  Blob blob; 
  image.magick( "JPEG" ) // Set JPEG output format 
  image.write( &blob );

  [ Use BLOB data (in JPEG format) here ]

  return 0; 
}
就个人而言,我可以推荐使用ImageMagick的这个库(尽管使用了C API…)


编辑:您可以将图像写入内存块,并将字节传递给编码器…

我不熟悉Windows-8编程,但只是查看文档,不是这么简单吗

Stream imageStreamSource = new FileStream("myimage.jpg" ...);
JpegBitmapDecoder decoder = new JpegBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
BitmapSource bitmapSource = decoder.Frames[0];
FileStream stream = new FileStream("myimage.png", FileMode.Create);
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Interlace = PngInterlaceOption.Off;
encoder.Frames.Add(bitmapSource);
encoder.Save(stream);

我不熟悉Windows-8编程,但只要看一下文档,不是这么简单吗

Stream imageStreamSource = new FileStream("myimage.jpg" ...);
JpegBitmapDecoder decoder = new JpegBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
BitmapSource bitmapSource = decoder.Frames[0];
FileStream stream = new FileStream("myimage.png", FileMode.Create);
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Interlace = PngInterlaceOption.Off;
encoder.Frames.Add(bitmapSource);
encoder.Save(stream);
使用

然后

BitmapEncoder::SetPixelData(BitmapPixelFormat pixelFormat、BitmapAlphaMode alphaMode alphaMode、无符号整数宽度、无符号整数高度、双dpiX、双dpiY、数组^pixels)
使用

然后

BitmapEncoder::SetPixelData(BitmapPixelFormat pixelFormat、BitmapAlphaMode alphaMode alphaMode、无符号整数宽度、无符号整数高度、双dpiX、双dpiY、数组^pixels)

<>代码> ,你可以读取像素数据,并使用给定的图像格式(编解码器)将它们写入到BITMAP编码器中(对不起,这是C++,而不是C++,但它应该工作):


唯一的问题是如何确定关于文件格式(在本例中为PNG)的像素格式、alpha模式和DPI的最佳设置。我根据现有的PNG文件确定了像素模式。设置了BitmapAlphaMode.Ignore,因为JPEG不支持透明度。因此,在PNG文件中启用alpha通道没有意义。宽度和高度设置为位图解码器的OrientedPixelWidth和OrientedPixelHeight,因为我在读取时启用了EXIF方向。关于目标系统,DPI设置得最好。96是默认的Windows。

< P>你可以读取像素数据,并使用给定的图像格式(编解码器)将它们写入到BITMAP编码器中(抱歉,这是C++,而不是C++,但它应该工作):


唯一的问题是如何确定关于文件格式(在本例中为PNG)的像素格式、alpha模式和DPI的最佳设置。我根据现有的PNG文件确定了像素模式。设置了BitmapAlphaMode.Ignore,因为JPEG不支持透明度。因此,在PNG文件中启用alpha通道没有意义。宽度和高度设置为位图解码器的OrientedPixelWidth和OrientedPixelHeight,因为我在读取时启用了EXIF方向。关于目标系统,DPI设置得最好。96是Windows的默认值。

这些类不适用于Windows应用商店应用。这些类不适用于Windows应用商店应用。
const string sourceFileName = "42.jpg";
StorageFolder imageFolder = Package.Current.InstalledLocation;
StorageFile imageFile = await imageFolder.GetFileAsync(sourceFileName);

using (IRandomAccessStream imageStream = await imageFile.OpenReadAsync())
{
  // Read the pixel data
  BitmapDecoder bitmapDecoder = await BitmapDecoder.CreateAsync(imageStream);
  BitmapTransform dummyTransform = new BitmapTransform();
  PixelDataProvider pixelDataProvider = await bitmapDecoder.GetPixelDataAsync(
     BitmapPixelFormat.Rgba8, BitmapAlphaMode.Ignore, dummyTransform,
     ExifOrientationMode.RespectExifOrientation,
     ColorManagementMode.ColorManageToSRgb);
  byte[] pixelData = pixelDataProvider.DetachPixelData();

  // Save the pixel data as PNG
  const string resultImageFileName = "42.png";
  StorageFile resultImageFile =
     await ApplicationData.Current.LocalFolder.CreateFileAsync(
     resultImageFileName, CreationCollisionOption.ReplaceExisting);
  using (IRandomAccessStream resultImageStream =
     await resultImageFile.OpenAsync(FileAccessMode.ReadWrite))
  {
     BitmapEncoder bitmapEncoder = await BitmapEncoder.CreateAsync(
        BitmapEncoder.PngEncoderId, resultImageStream);
     bitmapEncoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Ignore,
        bitmapDecoder.OrientedPixelWidth, bitmapDecoder.OrientedPixelHeight, 
        96, 96, pixelData);
     await bitmapEncoder.FlushAsync();
  }
}