C# 使用RenderTargetBitmap和PngBitmapEncoder时PNG大小的增长
我正在使用一种方法将一些平铺图像合并到单个图像中。但是如果我把它应用到四个30kb的PNG图像上,得到的PNG图像将是500K(比我预期的多4倍) 这是我正在使用的代码的一部分(由Cédric Bignon建议):C# 使用RenderTargetBitmap和PngBitmapEncoder时PNG大小的增长,c#,wpf,png,imaging,tiling,C#,Wpf,Png,Imaging,Tiling,我正在使用一种方法将一些平铺图像合并到单个图像中。但是如果我把它应用到四个30kb的PNG图像上,得到的PNG图像将是500K(比我预期的多4倍) 这是我正在使用的代码的一部分(由Cédric Bignon建议): 有人知道发生了什么吗?不知道原始图像很难说。PNG支持多种颜色模型和压缩。例如,如果4张原始图像有(不同的)调色板,构图必须采用真彩色格式,并且总大小可能是原始大小的4倍以上。结果图像的确切像素大小是多少?小图像是256*256,大图像是512*512这是可能的,平铺图像是压缩的,但
有人知道发生了什么吗?不知道原始图像很难说。PNG支持多种颜色模型和压缩。例如,如果4张原始图像有(不同的)调色板,构图必须采用真彩色格式,并且总大小可能是原始大小的4倍以上。结果图像的确切像素大小是多少?小图像是256*256,大图像是512*512这是可能的,平铺图像是压缩的,但结果图像不是。@Memorizer您能解释更多吗?Thank.PNG格式支持压缩:也许,平铺图像被压缩(这解释了小尺寸)原始图像是相同的(相同=调色板,…)两个4个子图像是相同的?他们脸色苍白吗?你有没有检查结果是托盘还是真彩色的?你的编辑没有回答我的问题。甚至不清楚你的断言“原始图像是相同的”是否意味着它们是相同的“类型”,或者它们是否相同。首先:“原始图像是相同的(相同=调色板…)”然后:“不知何故,相同的来源(??)…没有调色板”好吧,你没有让我们轻松。你最好附上图片。对不起!我使用了
JpegBitmapEncoder
而不是pngbitmapcoder
,得到的图像是126kb。所以我再次检查了原始图像。在windows属性中,它被写入项类型:PNG图像
和位深度:24
。当我在帧的CodecInfo
中运行代码时,它被写入FriendlyName=JPEG解码器
。所以我认为我之前的假设(基于Windows属性)是错误的。原始图像不是PNG。我说得对吗?
BitmapFrame frame1 = BitmapDecoder.Create(new Uri(path1), BitmapCreateOptions.None, BitmapCacheOption.OnLoad).Frames.First();
BitmapFrame frame2 = BitmapDecoder.Create(new Uri(path2), BitmapCreateOptions.None, BitmapCacheOption.OnLoad).Frames.First();
BitmapFrame frame3 = BitmapDecoder.Create(new Uri(path3), BitmapCreateOptions.None, BitmapCacheOption.OnLoad).Frames.First();
BitmapFrame frame4 = BitmapDecoder.Create(new Uri(path4), BitmapCreateOptions.None, BitmapCacheOption.OnLoad).Frames.First();
// Gets the size of the images (I assume each image has the same size)
int imageWidth = frame1.PixelWidth;
int imageHeight = frame1.PixelHeight;
// Draws the images into a DrawingVisual component
DrawingVisual drawingVisual = new DrawingVisual();
using (DrawingContext drawingContext = drawingVisual.RenderOpen())
{
drawingContext.DrawImage(frame1, new Rect(0, 0, imageWidth, imageHeight));
drawingContext.DrawImage(frame2, new Rect(imageWidth, 0, imageWidth, imageHeight));
drawingContext.DrawImage(frame3, new Rect(0, imageHeight, imageWidth, imageHeight));
drawingContext.DrawImage(frame4, new Rect(imageWidth, imageHeight, imageWidth, imageHeight));
}
// Converts the Visual (DrawingVisual) into a BitmapSource
RenderTargetBitmap bmp = new RenderTargetBitmap(imageWidth * 2, imageHeight * 2, 96, 96, PixelFormats.Pbgra32);
bmp.Render(drawingVisual);
// Creates a PngBitmapEncoder and adds the BitmapSource to the frames of the encoder
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmp));
// Saves the image into a file using the encoder
using (Stream stream = File.OpenWrite(pathTileImage))
encoder.Save(stream);