WPFで図形を描画し画像ファイルとして保存するプログラムです。
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
class Program
{
static void CreateRandomImage(string bmpPath, int width=3840, int height=2160)
{
var canvas = new Canvas();
canvas.Width = width;
canvas.Height = height;
canvas.Background = new SolidColorBrush(Colors.White);
Random rnd = new(); // Randomオブジェクトを作成
int stroke = (width < 1280) ? 1 : width / 1280;
int max = (width < 7) ? 512 : width / 7;
int min = (width < 30) ? 128 : width / 30;
for(int i = 0; i < 1024; i++)
{
// 四角形
Rectangle rectangle = new();
rectangle.Width = rnd.Next(min, max);
rectangle.Height= rnd.Next(min, max);
SolidColorBrush mySolidColorBrush = new ();
mySolidColorBrush.Color = Color.FromArgb(
(byte)rnd.Next(0,255), (byte)rnd.Next(0,255), (byte)rnd.Next(0,255), 127);
rectangle.Fill = mySolidColorBrush;
rectangle.Stroke = new SolidColorBrush(Colors.Black);
rectangle.StrokeThickness = stroke;
canvas.Children.Add(rectangle);
Canvas.SetLeft(rectangle, rnd.Next(1, (int)canvas.Width));
Canvas.SetTop(rectangle, rnd.Next(1, (int)canvas.Width));
// 多角形(三角形)
Polyline polyline = new();
polyline.Stroke = new SolidColorBrush(Colors.Black);
polyline.StrokeThickness = stroke;
polyline.FillRule = FillRule.EvenOdd;
Point point1 = new(rnd.Next(min, max), rnd.Next(min, max));
Point point2 = new(rnd.Next(min, max), rnd.Next(min, max));
Point point3 = new(rnd.Next(min, max), rnd.Next(min, max));
PointCollection pointCollection = new();
pointCollection.Add(point1);
pointCollection.Add(point2);
pointCollection.Add(point3);
polyline.Points = pointCollection;
SolidColorBrush mySolidColorBrush2 = new ();
mySolidColorBrush2.Color = Color.FromArgb(
(byte)rnd.Next(0,255), (byte)rnd.Next(0,255), (byte)rnd.Next(0,255), 127);
polyline.Fill = mySolidColorBrush2;
canvas.Children.Add(polyline);
Canvas.SetLeft(polyline, rnd.Next(1, (int)canvas.Width));
Canvas.SetTop(polyline, rnd.Next(1, (int)canvas.Width));
// 楕円
Ellipse ellipse = new();
ellipse.Stroke = new SolidColorBrush(Colors.Black);
ellipse.StrokeThickness = stroke;
SolidColorBrush mySolidColorBrush3 = new ();
mySolidColorBrush3.Color = Color.FromArgb(
(byte)rnd.Next(0,255), (byte)rnd.Next(0,255), (byte)rnd.Next(0,255), 127);
ellipse.Fill = mySolidColorBrush3;
ellipse.Width = rnd.Next(min, max);
ellipse.Height = rnd.Next(min, max);
canvas.Children.Add(ellipse);
Canvas.SetLeft(ellipse, rnd.Next(1, (int)canvas.Width));
Canvas.SetTop(ellipse, rnd.Next(1, (int)canvas.Width));
}
var size = new Size(canvas.Width, canvas.Height);
canvas.Measure(size);
canvas.Arrange(new Rect(size));
var renderBitmap = new RenderTargetBitmap(
(int)size.Width,
(int)size.Height,
96, 96, PixelFormats.Pbgra32);
renderBitmap.Render(canvas);
System.Windows.Media.Imaging.BitmapEncoder encoder;
using (var fs = new FileStream(bmpPath, FileMode.Create))
{
string ext = System.IO.Path.GetExtension(bmpPath).ToUpper();
if (ext == ".BMP")
{
encoder = new BmpBitmapEncoder();
}
else if (ext == ".JPG")
{
encoder = new JpegBitmapEncoder();
}
else if (ext == ".PNG")
{
encoder = new PngBitmapEncoder();
}
else
{
throw new FileFormatException(bmpPath);
}
encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
encoder.Save(fs);
}
}
[STAThread]
public static void Main()
{
const string imgPath = @"./sample.png";
const int width = 1280;
const int height = 720;
CreateRandomImage(imgPath, width, height);
}
}
プロジェクトはconsoleで作成しプロジェクトファイルを編集
TargetFrameworkをnet8.0-windows変更
UseWPFを値をtrueで追加。
実行するたびに異なる図形が描画されます。
テスト用の画像を沢山作りたい場合使おうと思います。
コメント