/// <summary> /// Encodes the specified image data to png. /// </summary> /// <param name="pixels"> /// The pixel data (bottom line first). /// </param> /// <param name="dpi"> /// The image resolution in dots per inch. /// </param> /// <returns> /// The png image data. /// </returns> public static byte[] Encode(OxyColor[,] pixels, int dpi = 96) { int height = pixels.GetLength(0); int width = pixels.GetLength(1); var bytes = new byte[(width * height * 4) + height]; int k = 0; for (int i = height - 1; i >= 0; i--) { bytes[k++] = 0; // Filter for (int j = 0; j < width; j++) { bytes[k++] = pixels[i, j].R; bytes[k++] = pixels[i, j].G; bytes[k++] = pixels[i, j].B; bytes[k++] = pixels[i, j].A; } } var w = new MemoryWriter(); w.Write((byte)0x89); w.Write("PNG\r\n\x1a\n".ToCharArray()); WriteChunk(w, "IHDR", CreateHeaderData(width, height)); WriteChunk(w, "pHYs", CreatePhysicalDimensionsData(dpi, dpi)); WriteChunk(w, "IDAT", CreateUncompressedBlocks(bytes)); WriteChunk(w, "IEND", new byte[0]); return w.ToArray(); }
/// <summary> /// Encodes the specified image data to png. /// </summary> /// <param name="pixels">The pixel data (bottom line first).</param> /// <returns>The png image data.</returns> public byte[] Encode(OxyColor[,] pixels) { int width = pixels.GetLength(0); int height = pixels.GetLength(1); var bytes = new byte[width * height * 4]; int k = 0; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { bytes[k++] = pixels[x, y].B; bytes[k++] = pixels[x, y].G; bytes[k++] = pixels[x, y].R; bytes[k++] = pixels[x, y].A; } } var ms = new MemoryStream(); var w = new BinaryWriter(ms); const int OffBits = 14 + 40; var size = OffBits + bytes.Length; // Bitmap file header (14 bytes) w.Write((byte)'B'); w.Write((byte)'M'); w.Write((uint)size); w.Write((ushort)0); w.Write((ushort)0); w.Write((uint)OffBits); // Bitmap info header (40 bytes) WriteBitmapInfoHeader(w, width, height, 32, bytes.Length, this.options.DpiX, this.options.DpiY); // Bitmap info V4 header (108 bytes) //// WriteBitmapV4Header(w, width, height, 32, bytes.Length, this.options.DpiX, this.options.DpiY); // Pixel array (from bottom-left corner) w.Write(bytes); return ms.ToArray(); }
/// <summary> /// Creates an <see cref="OxyImage"/> from the specified <see cref="OxyColor"/> array. /// </summary> /// <param name="data"> /// The pixel data, indexed as [row,column] (from bottom-left). /// </param> /// <param name="dpi"> /// The resolution. /// </param> /// <returns> /// An <see cref="OxyImage"/>. /// </returns> /// <remarks> /// This method is creating a simple BitmapInfoHeader. /// </remarks> public static OxyImage FromArgbX(OxyColor[,] data, int dpi = 96) { int height = data.GetLength(0); int width = data.GetLength(1); var bytes = new byte[width * height * 4]; int k = 0; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { bytes[k++] = data[i, j].B; bytes[k++] = data[i, j].G; bytes[k++] = data[i, j].R; bytes[k++] = data[i, j].A; } } return FromArgbX(width, height, bytes, dpi); }
/// <summary> /// Encodes the specified image data to png. /// </summary> /// <param name="pixels">The pixel data indexed as [x,y] (bottom line first).</param> /// <returns>The png image data.</returns> public byte[] Encode(OxyColor[,] pixels) { int width = pixels.GetLength(0); int height = pixels.GetLength(1); var bytes = new byte[(width * height * 4) + height]; int k = 0; for (int y = 0; y < height; y++) { bytes[k++] = 0; // Filter for (int x = 0; x < width; x++) { bytes[k++] = pixels[x, y].R; bytes[k++] = pixels[x, y].G; bytes[k++] = pixels[x, y].B; bytes[k++] = pixels[x, y].A; } } var w = new MemoryWriter(); w.Write((byte)0x89); w.Write("PNG\r\n\x1a\n".ToCharArray()); WriteChunk(w, "IHDR", CreateHeaderData(width, height)); WriteChunk(w, "pHYs", CreatePhysicalDimensionsData(this.options.DpiX, this.options.DpiY)); WriteChunk(w, "IDAT", CreateUncompressedBlocks(bytes)); WriteChunk(w, "IEND", new byte[0]); return w.ToArray(); }