- public static BitmapSource Create(
- int pixelWidth,
- int pixelHeight,
- double dpiX,
- double dpiY,
- PixelFormat pixelFormat,
- BitmapPalette palette,
- Array pixels,
- int stride
- )
- using System.IO;
- using System.Windows.Forms;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- namespace WindowsFormsApp1
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, System.EventArgs e)
- {
- int width = 256;
- int height = 256;
- // 0〜255のグレースケール画像
- byte[] data = new byte[width * height];
- // 画像の上から順に0から255へグラデーション
- for (int h = 0; h < height; h++)
- {
- for (int w = 0; w < width; w++)
- {
- data[h * width + w] = (byte)h;
- }
- }
- BitmapSource image = BitmapSource.Create(
- width,
- height,
- 96, // DPI x
- 96, // DPI y
- PixelFormats.Indexed8,
- BitmapPalettes.Gray256,
- data,
- width);
- FileStream stream = new FileStream(@"c:\dev\new.png", FileMode.Create);
- PngBitmapEncoder encoder = new PngBitmapEncoder();
- encoder.Interlace = PngInterlaceOption.On;
- encoder.Frames.Add(BitmapFrame.Create(image));
- encoder.Save(stream);
- }
- }
- }
- using System.IO;
- using System.Windows.Forms;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- namespace WindowsFormsApp1
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, System.EventArgs e)
- {
- int width = 256;
- int height = 256;
- // 0〜255のグレースケール画像
- byte[] data = new byte[width * height];
- // 画像の上から順に0から255へグラデーション
- for (int h = 0; h < height; h++)
- {
- for (int w = 0; w < width; w++)
- {
- data[h * width + w] = (byte)h;
- }
- }
- BitmapSource image = BitmapSource.Create(
- width,
- height,
- 96, // DPI x
- 96, // DPI y
- PixelFormats.Indexed8,
- BitmapPalettes.Gray256,
- data,
- width);
- /* png
- * FileStream stream = new FileStream(@"c:\dev\new.png", FileMode.Create);
- PngBitmapEncoder encoder = new PngBitmapEncoder();
- encoder.Interlace = PngInterlaceOption.On;
- */
- FileStream stream = new FileStream(@"c:\dev\new.jpg", FileMode.Create);
- JpegBitmapEncoder encoder = new JpegBitmapEncoder();
- encoder.QualityLevel = 100;
- encoder.Frames.Add(BitmapFrame.Create(image));
- encoder.Save(stream);
- }
- }
- }
- using System.IO;
- using System.Windows.Forms;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- namespace WindowsFormsApp1
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, System.EventArgs e)
- {
- int width = 256;
- int height = 256;
- // 0〜255のグレースケール画像
- byte[] data = new byte[width * height];
- // 画像の上から順に0から255へグラデーション
- for (int h = 0; h < height; h++)
- {
- for (int w = 0; w < width; w++)
- {
- data[h * width + w] = (byte)h;
- }
- }
- BitmapSource image = BitmapSource.Create(
- width,
- height,
- 96, // DPI x
- 96, // DPI y
- PixelFormats.Indexed8,
- BitmapPalettes.Gray256,
- data,
- width);
- /* png
- * FileStream stream = new FileStream(@"c:\dev\new.png", FileMode.Create);
- PngBitmapEncoder encoder = new PngBitmapEncoder();
- encoder.Interlace = PngInterlaceOption.On;
- */
- /*
- FileStream stream = new FileStream(@"c:\dev\new.jpg", FileMode.Create);
- JpegBitmapEncoder encoder = new JpegBitmapEncoder();
- encoder.QualityLevel = 100;
- */
- FileStream stream = new FileStream(@"c:\dev\new.tif", FileMode.Create);
- TiffBitmapEncoder encoder = new TiffBitmapEncoder();
- encoder.Compression = TiffCompressOption.Zip;
- encoder.Frames.Add(BitmapFrame.Create(image));
- encoder.Save(stream);
- }
- }
- }
Author:symfo
blog形式だと探しにくいので、まとめサイト作成中です。
Symfoware まとめ