2011-07-30 19 views
4

フォームのサイズを変更すると、画像をピクチャボックスの中央に配置するにはどうすればよいですか?私が持っているのはパネル内のピクチャボックスなので、画像がピクチャボックスよりも大きければ、パネル上にスクロールバーが表示されます。しかし、これはピクチャボックスサイズモード "中央画像"では機能せず、 "自動サイズ"でのみ機能します。サイズ変更時に画像をpictureboxに配置するにはどうすればいいですか?

答えて

15

ここでPictureBoxを使用しないでください。Panelは、既にBackgroundImageプロパティを使用して中央の画像を表示することができます。必要なのは、DoubleBufferedプロパティをオンにしてフリッカーを抑制することだけです。プロジェクトに新しいクラスを追加し、以下に示すコードを貼り付けます。コンパイル。新しいコントロールをツールボックスの上部からフォームにドラッグし、パネルを置き換えます。 Propertiesウィンドウまたはコード内にBackgroundImageプロパティを割り当てます。

using System; 
using System.Drawing; 
using System.Windows.Forms; 

internal class PicturePanel : Panel { 
    public PicturePanel() { 
     this.DoubleBuffered = true; 
     this.AutoScroll = true; 
     this.BackgroundImageLayout = ImageLayout.Center; 
    } 
    public override Image BackgroundImage { 
     get { return base.BackgroundImage; } 
     set { 
      base.BackgroundImage = value; 
      if (value != null) this.AutoScrollMinSize = value.Size; 
     } 
    } 
} 
0

Paddingを使用すると何が問題になりますか?

void picturebox_Paint(object sender, PaintEventArgs e) 
{ 
    int a = picturebox.Width - picturebox.Image.Width; 
    int b = picturebox.Height - picturebox.Image.Height; 
    Padding p = new System.Windows.Forms.Padding(); 
    p.Left = a/2; 
    p.Top = b/2; 
    picturebox.Padding = p; 
} 
0

これは、簡単のSizeModeプロパティで

pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage; 
を行うことができます
関連する問題