If you have a large horizontal image Nx16 and you’d like to split it into individual 16x16 icons, here’s how to do it in WPF. I had to look it up recently and nothing showed up, so it was faster to write it myself. I’m posting it here so that it’s easy to find in the future.
using System.IO;
using System.Windows;
using System.Windows.Media.Imaging;
class Program
{
static void Main(string[] args)
{
var file = @"Imagestrip1600x16.png";
var output = @"IconsFolder";
using (Stream imageStreamSource = new FileStream(
file, FileMode.Open, FileAccess.Read, FileShare.Read))
{
PngBitmapDecoder decoder = new PngBitmapDecoder(
imageStreamSource,
BitmapCreateOptions.PreservePixelFormat,
BitmapCacheOption.Default);
BitmapSource bitmapSource = decoder.Frames[0];
for (int i = 0; i < bitmapSource.PixelWidth / 16; i++)
{
CroppedBitmap croppedBitmap = new CroppedBitmap(
bitmapSource,
new Int32Rect(i * 16, 0, 16, 16));
PngBitmapEncoder encoder = new PngBitmapEncoder();
var frame = BitmapFrame.Create(croppedBitmap);
encoder.Frames.Add(frame);
var fileName = Path.Combine(output, i.ToString() + ".png");
using (var stream = new FileStream(fileName, FileMode.Create))
{
encoder.Save(stream);
Read more: Kirill Osenkov
QR:
0 comments:
Post a Comment