using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace _20180416_拡大縮小_最近傍補間法
{
public partial class MainWindow : Window
{
BitmapSource OriginBitmap;
string ImageFileFullPath;
public MainWindow()
{
InitializeComponent();
Title = this.ToString();
AllowDrop = true;
Drop += MainWindow_Drop;
SliderXScale.MouseWheel += SliderScale_MouseWheel;
SliderYScale.MouseWheel += SliderScale_MouseWheel;
MyButtonSave.Click += MyButtonSave_Click;
MyButtonOrigin.Click += MyButtonOrigin_Click;
MyButton2x2.Click += MyButton2x2_Click;
MyButton1.Click += MyButton1_Click;
MyButton2.Click += MyButton2_Click;
}
private void MyButtonSave_Click(object sender, RoutedEventArgs e)
{
if (OriginBitmap == null) { return; }
SaveImage((BitmapSource)MyImage.Source);
}
private void MyButtonOrigin_Click(object sender, RoutedEventArgs e)
{
if (OriginBitmap == null) { return; }
MyImage.Source = OriginBitmap;
}
private void MyButton2x2_Click(object sender, RoutedEventArgs e)
{
if (OriginBitmap == null) { return; }
MyImage.Source = F4今表示している画像をニアレストネイバー法で2倍((BitmapSource)MyImage.Source);
}
private void SliderScale_MouseWheel(object sender, MouseWheelEventArgs e)
{
Slider s = (Slider)sender;
if (e.Delta > 0) { s.Value += 0.1; }
else { s.Value -= 0.1; }
}
private void MyButton2_Click(object sender, RoutedEventArgs e)
{
if (OriginBitmap == null) { return; }
MyImage.Source = F2最近傍補間法カラー(OriginBitmap, SliderXScale.Value, SliderYScale.Value);
}
private void MyButton1_Click(object sender, RoutedEventArgs e)
{
if (OriginBitmap == null) { return; }
var b = new FormatConvertedBitmap(OriginBitmap, PixelFormats.Gray8, null, 0);
MyImage.Source = F1最近傍補間法グレースケール(b, SliderXScale.Value, SliderYScale.Value);
}
private BitmapSource F4今表示している画像をニアレストネイバー法で2倍(BitmapSource source)
{
if (source.PixelHeight > 1000)
{
if (MessageBox.Show(
"縦ピクセル2000以上の大きな画像になる、実行する?",
"処理実行確認",
MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
{
return source;
}
}
var wb = new WriteableBitmap(source);
int h = wb.PixelHeight;
int w = wb.PixelWidth;
int stride = wb.BackBufferStride;
var pixels = new byte[h * stride];
wb.CopyPixels(pixels, stride, 0);
int nH = h * 2;
int nW = w * 2;
var nWb = new WriteableBitmap(nW, nH, 96, 96, source.Format, source.Palette);
int nStride = nWb.BackBufferStride;
var nPixels = new byte[nH * nStride];
long nP = 0, p = 0;
int cc = source.Format.BitsPerPixel / 8;
for (int y = 0; y < nH; ++y)
{
for (int x = 0; x < nW; ++x)
{
nP = y * nStride + (x * cc);
p = (y / 2) * stride + ((x / 2) * cc);
for (int i = 0; i < cc; ++i)
{
nPixels[nP + i] = pixels[p + i];
}
}
}
nWb.WritePixels(new Int32Rect(0, 0, nW, nH), nPixels, nStride, 0);
return nWb;
}
<summary>
</summary>
<param name="source"></param>
<param name="xScale"></param>
<param name="yScale"></param>
<returns></returns>
private BitmapSource F2最近傍補間法カラー(BitmapSource source, double xScale, double yScale)
{
var wb = new WriteableBitmap(source);
int h = wb.PixelHeight;
int w = wb.PixelWidth;
int stride = wb.BackBufferStride;
var pixels = new byte[h * stride];
wb.CopyPixels(pixels, stride, 0);
long p = 0;
int nH = (int)(Math.Round((h * yScale), MidpointRounding.AwayFromZero));
int nW = (int)(Math.Round((w * xScale), MidpointRounding.AwayFromZero));
var nWb = new WriteableBitmap(nW, nH, 96, 96, source.Format, source.Palette);
int nStride = nWb.BackBufferStride;
var nPixels = new byte[nH * nStride];
long nP = 0;
int motoX, motoY;
for (int y = 0; y < nH; ++y)
{
for (int x = 0; x < nW; ++x)
{
motoX = (int)(x * (w / (float)nW));
motoY = (int)(y * (h / (float)nH));
p = motoY * stride + (motoX * 4);
nP = y * nStride + (x * 4);
nPixels[nP + 3] = pixels[p + 3];
nPixels[nP + 2] = pixels[p + 2];
nPixels[nP + 1] = pixels[p + 1];
nPixels[nP] = pixels[p];
}
}
nWb.WritePixels(new Int32Rect(0, 0, nW, nH), nPixels, nStride, 0);
return nWb;
}
private BitmapSource F1最近傍補間法グレースケール(BitmapSource source, double xScale, double yScale)
{
var wb = new WriteableBitmap(source);
int h = wb.PixelHeight;
int w = wb.PixelWidth;
int stride = wb.BackBufferStride;
var pixels = new byte[h * stride];
wb.CopyPixels(pixels, stride, 0);
long p = 0;
int nH = (int)(Math.Round((h * yScale), MidpointRounding.AwayFromZero));
int nW = (int)(Math.Round((w * xScale), MidpointRounding.AwayFromZero));
var nWb = new WriteableBitmap(nW, nH, 96, 96, source.Format, source.Palette);
int nStride = nWb.BackBufferStride;
var nPixels = new byte[nH * nStride];
long nP = 0;
double xx, yy;
int motoX, motoY;
for (int y = 0; y < nH; ++y)
{
for (int x = 0; x < nW; ++x)
{
xx = x * (w / (float)nW);
yy = y * (h / (float)nH);
motoX = (int)xx;
motoY = (int)yy;
p = motoY * stride + motoX;
nP = y * nStride + (x * 1);
nPixels[nP] = pixels[p];
}
}
nWb.WritePixels(new Int32Rect(0, 0, nW, nH), nPixels, nStride, 0);
return nWb;
}
private void MainWindow_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop) == false) { return; }
string[] filePath = (string[])e.Data.GetData(DataFormats.FileDrop);
OriginBitmap = GetBitmapSourceWithChangePixelFormat2(filePath[0], PixelFormats.Pbgra32, 96, 96);
if (OriginBitmap == null)
{
MessageBox.Show("not Image");
}
else
{
MyImage.Source = OriginBitmap;
ImageFileFullPath = filePath[0];
}
}
private void SaveImage(BitmapSource source)
{
var saveFileDialog = new Microsoft.Win32.SaveFileDialog();
saveFileDialog.Filter = "*.png|*.png|*.bmp|*.bmp|*.tiff|*.tiff|*.jpg|*.jpg";
saveFileDialog.AddExtension = true;
saveFileDialog.FileName = System.IO.Path.GetFileNameWithoutExtension(ImageFileFullPath) + "_";
saveFileDialog.InitialDirectory = System.IO.Path.GetDirectoryName(ImageFileFullPath);
if (saveFileDialog.ShowDialog() == true)
{
BitmapEncoder encoder = new BmpBitmapEncoder();
if (saveFileDialog.FilterIndex == 1)
{
encoder = new PngBitmapEncoder();
}
else if (saveFileDialog.FilterIndex == 2)
{
encoder = new BmpBitmapEncoder();
}
else if (saveFileDialog.FilterIndex == 3)
{
encoder = new TiffBitmapEncoder();
}
else if (saveFileDialog.FilterIndex == 4)
{
var je = new JpegBitmapEncoder();
je.QualityLevel = 96;
encoder = je;
}
encoder.Frames.Add(BitmapFrame.Create(source));
using (var fs = new System.IO.FileStream(saveFileDialog.FileName, System.IO.FileMode.Create, System.IO.FileAccess.Write))
{
encoder.Save(fs);
}
}
}
<summary>
</summary>
<param name="filePath"></param>
<param name="pixelFormat"></param>
<param name="dpiX"></param>
<param name="dpiY"></param>
<returns></returns>
private BitmapSource GetBitmapSourceWithChangePixelFormat2(
string filePath, PixelFormat pixelFormat, double dpiX = 0, double dpiY = 0)
{
BitmapSource source = null;
try
{
using (System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
var bf = BitmapFrame.Create(fs);
var convertedBitmap = new FormatConvertedBitmap(bf, pixelFormat, null, 0);
int w = convertedBitmap.PixelWidth;
int h = convertedBitmap.PixelHeight;
int stride = (w * pixelFormat.BitsPerPixel + 7) / 8;
byte[] pixels = new byte[h * stride];
convertedBitmap.CopyPixels(pixels, stride, 0);
if (dpiX == 0) { dpiX = bf.DpiX; }
if (dpiY == 0) { dpiY = bf.DpiY; }
source = BitmapSource.Create(
w, h, dpiX, dpiY,
convertedBitmap.Format,
convertedBitmap.Palette, pixels, stride);
};
}
catch (Exception)
{
}
return source;
}
}
}