public Write ( byte buffer, int offset, int count ) : void | ||
buffer | byte | The buffer holding data to write to the stream. |
offset | int | the offset within that data array to find the first byte to write. |
count | int | the number of bytes to write. |
return | void |
private static void WriteDataChunks(Stream outputStream, WriteableBitmap writeableBitmap, WriteableBitmapSavePngParameters parameters) { using (var chunkedStream = new ChunkedStream(MaximumChunkSize, data => WriteChunk(outputStream, PngChunkTypeData, data))) { using (var zlibStream = new ZlibStream(chunkedStream, CompressionMode.Compress, parameters.CompressionLevel, true)) { writeableBitmap.Invalidate(); var pixels = writeableBitmap.Pixels; var width = writeableBitmap.PixelWidth; var height = writeableBitmap.PixelHeight; var index = 0; var dataRowLength = width * 4; var dataRow = new byte[dataRowLength]; for (var y = 0; y < height; y++) { zlibStream.WriteByte(0); for (var x = 0; x < width; x++) { var color = pixels[index++]; var alpha = (byte)(color >> 24); int alphaInt = alpha; if (alphaInt == 0) { alphaInt = 1; } alphaInt = (255 << 8) / alphaInt; var dataRowOffset = x * 4; dataRow[dataRowOffset] = (byte)((((color >> 16) & 0xFF) * alphaInt) >> 8); dataRow[dataRowOffset + 1] = (byte)((((color >> 8) & 0xFF) * alphaInt) >> 8); dataRow[dataRowOffset + 2] = (byte)(((color & 0xFF) * alphaInt) >> 8); dataRow[dataRowOffset + 3] = alpha; } zlibStream.Write(dataRow, 0, dataRowLength); } } } }