C#で画像の拡大・縮小の手順を紹介します。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ImageResize
{
public partial class Form_Main : Form
{
public Form_Main()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
textBox1.Text = openFileDialog1.FileName;
}
}
private void button2_Click(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(textBox1.Text);
int resizeWidth = 160;
int resizeHeight = (int)(bmp.Height * ((double)resizeWidth / (double)bmp.Width));
Bitmap resizeBmp = new Bitmap(resizeWidth, resizeHeight);
Graphics g = Graphics.FromImage(resizeBmp);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
//g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
g.DrawImage(bmp, 0, 0, resizeWidth, resizeHeight);
g.Dispose();
Graphics pg = Graphics.FromHwnd(panel1.Handle);
pg.DrawImage(resizeBmp, new Point(0, 0));
}
}
}
にてTextBoxに入力されたパスの画像ファイルを読み込みます。
画像ファイルのファイル名をテキストボックスに入力してButton2をクリックすると縮小画像をフォームに表示します。