private static void WriteOutputImage(string outputFile, OutputType outputType, int width, int height, byte[] bitmap) { Console.WriteLine("Writing {0}", outputFile); using (var stream = File.Create(outputFile)) { var imageWriter = new ImageWriter(); switch (outputType) { case OutputType.Jpg: imageWriter.WriteJpg(bitmap, width, height, StbImageWriteSharp.ColorComponents.RedGreenBlueAlpha, stream, 90); break; case OutputType.Png: imageWriter.WritePng(bitmap, width, height, StbImageWriteSharp.ColorComponents.RedGreenBlueAlpha, stream); break; case OutputType.Tga: imageWriter.WriteTga(bitmap, width, height, StbImageWriteSharp.ColorComponents.RedGreenBlueAlpha, stream); break; case OutputType.Bmp: imageWriter.WriteBmp(bitmap, width, height, StbImageWriteSharp.ColorComponents.RedGreenBlueAlpha, stream); break; case OutputType.Hdr: imageWriter.WriteHdr(bitmap, width, height, StbImageWriteSharp.ColorComponents.RedGreenBlueAlpha, stream); break; } } }
public void SaveToFile(string path) { using var stream = File.OpenWrite(path); var image_writer = new ImageWriter(); image_writer.WritePng(pixel_data, Width, Height, ColorComponents.RedGreenBlueAlpha, stream); }
public void SaveToFile(string path) { var image_writer = new ImageWriter(); SwizzleToRGBA(); image_writer.WritePng(this, path); SwizzleToBGRA(); }
public void SaveToFile(string path) { var image_writer = new ImageWriter(); SwizzleToRGBA(); image_writer.WritePng(this.PixelData, this.Width, this.Height, path); SwizzleToBGRA(); }
public void Save(string path) { using Stream stream = File.OpenWrite(path); var writer = new ImageWriter(); writer.WritePng( this._data, this.Width, this.Height, StbImageWriteSharp.ColorComponents.RedGreenBlueAlpha, stream); }
private unsafe void SaveAsImage(Stream stream, int width, int height, ImageWriterFormat format) { if (stream == null) { throw new ArgumentNullException("stream", "'stream' cannot be null (Nothing in Visual Basic)"); } if (width <= 0) { throw new ArgumentOutOfRangeException("width", width, "'width' cannot be less than or equal to zero"); } if (height <= 0) { throw new ArgumentOutOfRangeException("height", height, "'height' cannot be less than or equal to zero"); } Color[] data = null; try { data = GetColorData(); // Write fixed(Color *ptr = &data[0]) { var writer = new ImageWriter(); switch (format) { case ImageWriterFormat.Jpg: writer.WriteJpg(ptr, width, height, StbImageWriteSharp.ColorComponents.RedGreenBlueAlpha, stream, 90); break; case ImageWriterFormat.Png: writer.WritePng(ptr, width, height, StbImageWriteSharp.ColorComponents.RedGreenBlueAlpha, stream); break; } } } finally { if (data != null) { data = null; } } }
static void Main(string[] args) { if (args.Length < 2) { Console.WriteLine("Usage: StbImageResizeSharp.Converter.exe <input_image_file> <output_image_file> [new_width] [new_height]"); Console.WriteLine("Example: StbImageResizeSharp.Converter.exe image.jpg image.png 400 400"); return; } try { OutputType outputType = OutputType.Png; var ext = Path.GetExtension(args[1]); if (string.IsNullOrEmpty(ext)) { throw new Exception("Output file lacks extension. Hence it is not possible to determine output file type"); } if (ext.StartsWith(".")) { ext = ext.Substring(1); } ext = ext.ToLower(); switch (ext) { case "jpg": case "jpeg": outputType = OutputType.Jpg; break; case "png": outputType = OutputType.Png; break; case "tga": outputType = OutputType.Tga; break; case "bmp": outputType = OutputType.Bmp; break; case "hdr": outputType = OutputType.Hdr; break; default: throw new Exception("Output format '" + ext + "' is not supported."); } // Load image ImageResult image; using (var stream = File.OpenRead(args[0])) { image = ImageResult.FromStream(stream); } // Parse new size var newWidth = image.Width; var newHeight = image.Height; if (args.Length >= 3) { newWidth = int.Parse(args[2]); } if (args.Length >= 4) { newHeight = int.Parse(args[3]); } var newData = image.Data; var channels = (int)image.Comp; // Resize if needed if (newWidth != image.Width && newHeight != image.Height) { newData = new byte[newWidth * newHeight * channels]; StbImageResize.stbir_resize_uint8(image.Data, image.Width, image.Height, image.Width * channels, newData, newWidth, newHeight, newWidth * channels, channels); } // Save using (var stream = File.Create(args[1])) { var imageWriter = new ImageWriter(); switch (outputType) { case OutputType.Jpg: imageWriter.WriteJpg(newData, newWidth, newHeight, (StbImageWriteSharp.ColorComponents)channels, stream, 90); break; case OutputType.Png: imageWriter.WritePng(newData, newWidth, newHeight, (StbImageWriteSharp.ColorComponents)channels, stream); break; case OutputType.Tga: imageWriter.WriteTga(newData, newWidth, newHeight, (StbImageWriteSharp.ColorComponents)channels, stream); break; case OutputType.Bmp: imageWriter.WriteBmp(newData, newWidth, newHeight, (StbImageWriteSharp.ColorComponents)channels, stream); break; case OutputType.Hdr: imageWriter.WriteHdr(newData, newWidth, newHeight, (StbImageWriteSharp.ColorComponents)channels, stream); break; } } } catch (Exception ex) { Console.WriteLine(ex); } }
private void buttonSave_Click(object sender, EventArgs e) { try { string fileName; using (var dlg = new SaveFileDialog()) { dlg.Filter = "BMP Files (*.bmp)|*.bmp|TGA Files (*.tga)|*.tga|PNG Files (*.png)|*.png|HDR Files (*.hdr)|*.hdr|JPG Files (*.jpg)|*.jpg"; if (dlg.ShowDialog() != DialogResult.OK) { return; } fileName = dlg.FileName; } // Get bitmap bytes var bmp = (Bitmap)pictureBox1.Image; var x = bmp.Width; var y = bmp.Height; var bmpData = bmp.LockBits(new Rectangle(0, 0, x, y), ImageLockMode.ReadOnly, bmp.PixelFormat); var data = new byte[y * bmpData.Stride]; Marshal.Copy(bmpData.Scan0, data, 0, bmpData.Stride * bmp.Height); bmp.UnlockBits(bmpData); // Convert bgra to rgba for (var i = 0; i < x * y; ++i) { var b = data[i * 4]; var g = data[i * 4 + 1]; var r = data[i * 4 + 2]; var a = data[i * 4 + 3]; data[i * 4] = r; data[i * 4 + 1] = g; data[i * 4 + 2] = b; data[i * 4 + 3] = a; } // Call StbSharp using (var stream = File.Create(fileName)) { var writer = new ImageWriter(); var image = new Image { Data = data, Width = x, Height = y, Comp = 4 }; if (fileName.EndsWith(".bmp")) { writer.WriteBmp(image, stream); } else if (fileName.EndsWith(".tga")) { writer.WriteTga(image, stream); } else if (fileName.EndsWith("png")) { writer.WritePng(image, stream); } else if (fileName.EndsWith("hdr")) { writer.WriteHdr(image, stream); } else if (fileName.EndsWith("jpg") || fileName.EndsWith("jpeg")) { writer.WriteJpg(image, stream, 75); } } } catch (Exception ex) { MessageBox.Show("Error", ex.Message); } }