PictureBox で最近傍リサンプリングを使用するにはどうすればよいですか?
-
09-06-2019 - |
質問
ボックスはスプリッターでサイズ変更できるため、StretchImage を使用しています。デフォルトはある種の滑らかな双線形フィルタリングのようで、画像がぼやけてモアレ パターンが発生します。
解決
この機能も必要でした。PictureBoxを継承してオーバーライドするクラスを作りました OnPaint
そして、補間モードを設定できるようにするプロパティを追加します。
using System.Drawing.Drawing2D;
using System.Windows.Forms;
/// <summary>
/// Inherits from PictureBox; adds Interpolation Mode Setting
/// </summary>
public class PictureBoxWithInterpolationMode : PictureBox
{
public InterpolationMode InterpolationMode { get; set; }
protected override void OnPaint(PaintEventArgs paintEventArgs)
{
paintEventArgs.Graphics.InterpolationMode = InterpolationMode;
base.OnPaint(paintEventArgs);
}
}
他のヒント
Image クラスと DrawImage 関数を使用して手動でサイズ変更を行い、PictureBox のサイズ変更イベントに応答する必要があると思います。
MSDN 検索を行ったところ、これに関する記事があることがわかりました。これはあまり詳細ではありませんが、ペイント イベントを使用する必要があることの概要を示しています。
http://msdn.microsoft.com/en-us/library/k0fsyd4e.aspx
この機能を使用するために、一般に入手可能な画像ズームの例を編集しました。以下を参照してください。
編集元: http://www.dotnetcurry.com/ShowArticle.aspx?ID=196&AspxAutoDetectCookieSupport=1
お役に立てれば
private void Form1_Load(object sender, EventArgs e)
{
// set image location
imgOriginal = new Bitmap(Image.FromFile(@"C:\images\TestImage.bmp"));
picBox.Image = imgOriginal;
// set Picture Box Attributes
picBox.SizeMode = PictureBoxSizeMode.StretchImage;
// set Slider Attributes
zoomSlider.Minimum = 1;
zoomSlider.Maximum = 5;
zoomSlider.SmallChange = 1;
zoomSlider.LargeChange = 1;
zoomSlider.UseWaitCursor = false;
SetPictureBoxSize();
// reduce flickering
this.DoubleBuffered = true;
}
// picturebox size changed triggers paint event
private void SetPictureBoxSize()
{
Size s = new Size(Convert.ToInt32(imgOriginal.Width * zoomSlider.Value), Convert.ToInt32(imgOriginal.Height * zoomSlider.Value));
picBox.Size = s;
}
// looks for user trackbar changes
private void trackBar1_Scroll(object sender, EventArgs e)
{
if (zoomSlider.Value > 0)
{
SetPictureBoxSize();
}
}
// redraws image using nearest neighbour resampling
private void picBox_Paint_1(object sender, PaintEventArgs e)
{
e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
e.Graphics.DrawImage(
imgOriginal,
new Rectangle(0, 0, picBox.Width, picBox.Height),
// destination rectangle
0,
0, // upper-left corner of source rectangle
imgOriginal.Width, // width of source rectangle
imgOriginal.Height, // height of source rectangle
GraphicsUnit.Pixel);
}
.net で画像のサイズを変更する場合、System.Drawing.Drawing2D.InterpolationMode は次のサイズ変更メソッドを提供します。
- バイキュービック
- バイリニア
- 高い
- 高品質バイキュービック
- 高品質バイリニア
- 低い
- 最も近い隣人
- デフォルト
所属していません StackOverflow