2012-02-21 69 views
7

基本的な画像処理を行うことができる単純な画像ビューアを設計しています。現時点では、PictureBoxを常にTabPageの中心に置き、ピクチャボックスの幅とサイズを画像の表示と同じに保つという問題があります。今まで私は成功しなかった。PictureBoxをコンテナの中央に配置する

私はフォームコンストラクタをコールして中央に配置するコードを持っています。 (あなたが戻ってPictureBoxコントロールの色である黒の一部を見ることができます)

private void SetPictureBoxOriginalSizeAndLocation(bool makeImageNull = false, DockStyle dockStyle = DockStyle.None) 
{ 
    if (makeImageNull) picBoxView.Image = null; 
    picBoxView.Dock = dockStyle; 

    var xPoint = tabImageView.Location.X + ((splitContainer.Panel2.Width/2)/2); 
    var yPoint = tabImageView.Location.Y; 

    var width = tabImageView.Width/2; 
    var height = (tabImageView.Height/2) - toolStripImageView.Height; 

    if (picBoxView.Image == null) return; 

    //Resize image according to width 
    picBoxView.Image = ImageMethods.ResizeImage(picBoxView.Image.Tag.ToString(), width, height, false); 

    picBoxView.Location = new Point(xPoint, yPoint); 
    picBoxView.Width = width; 
    picBoxView.Height = height; 
} 

しかし、それは、その画像にピクチャボックスのサイズを変更しません:それは、ピクチャボックスを中央にするのは初めての作品

IT is ok the first time

問題は、すぐに私は、フォームのサイズを変更すると悪化している、のPictureBox位置は上に行くだろう:

Form resized

私はフォームのサイズ変更イベントでも上記のコードを呼び出しますが、なぜアプリケーションが起動するときに動作するのかわかりません。誰かが私が自分のイメージと同じ大きさのきれいな中心の絵文字を作るために気をつけなければならないものを私に教えてもらえればいいでしょう。

+0

Dockを塗りつぶすように設定し、イメージを背景イメージとして設定してください。 BackgroundImageLayoutはCenterに設定できます。そしてBackGroundColorを透明にする –

+0

'picturebox.Image'サイズは' picturebox'サイズと等しくなりますか?私はイメージのピクセルで作業する必要があるため、ピクチャボックスがピクチャよりも大きいかどうかわかりません。私が作業しているイメージはピクチャボックス自体になりますか?私が知っている愚かな音! –

+0

しかし、私は再び写真を拡大したいと思ったら問題があると思います! –

答えて

14

private void CenterPictureBox(PictureBox picBox, Bitmap picImage) { 
    picBox.Image = picImage; 
    picBox.Location = new Point((picBox.Parent.ClientSize.Width/2) - (picImage.Width/2), 
           (picBox.Parent.ClientSize.Height/2) - (picImage.Height/2)); 
    picBox.Refresh(); 
} 
:あなたは PictureBoxの画像を変更するたび

picBoxView = new PictureBox(); 
picBoxView.SizeMode = PictureBoxSizeMode.AutoSize; 
picBoxView.Anchor = AnchorStyles.None; 
tabImageView.Controls.Add(picBoxView); 
CenterPictureBox(picBoxView, myImage); 

それからちょうど最初にPictureBoxを中心に

を持つとAnchor = Noneはセント親コンテナがデフォルトの左と上の位置にアンカーされているため、親コンテナのサイズが変更されたときはいつでも、PictureBoxコントロールを使用できます。

1

Givien以下、FillDockセットを持っていTabControlFormは、中央にあなたのPictureBoxを維持します。また、BitmapサイズにPictureBoxサイズを設定します:

public partial class Form1 : Form 
    { 
     Bitmap b = new Bitmap(320, 200); 
     public Form1() 
     { 
      InitializeComponent(); 
      CenterTheBox(); 
     } 

     private void Form1_Resize(object sender, EventArgs e) 
     { 
      CenterTheBox(); 
     } 

     void CenterTheBox() 
     { 
      pictureBox1.Size = b.Size; 
      var left = (tabPage1.ClientRectangle.Width - pictureBox1.ClientRectangle.Width)/2; 
      var top = (tabPage1.ClientRectangle.Height - pictureBox1.ClientRectangle.Height)/2; 
      pictureBox1.Location = new Point(tabPage1.ClientRectangle.Location.X + left, tabPage1.ClientRectangle.Location.Y + top); 

     } 
    } 
1

私はあなたの問題は、それが

​​

に設定する必要がありますalthought

var xPoint = tabImageView.Location.X + ((splitContainer.Panel2.Width/2)/2); 
var yPoint = tabImageView.Location.Y; 

var width = tabImageView.Width/2; 
var height = (tabImageView.Height/2) - toolStripImageView.Height; 

ypointがalwways、tabImageView Yに設定されているここにありと信じてxPointとほぼ同じにする必要があります

tabImageView.Location.X + (tabImageView.Size.Width - picBoxView.Size.Width)/2 

とあなただけのnoneにAnchorスタイル設定した場合、それは非常に簡単です

width = picBoxView.Image.Width; 
height = picBoxView.Image.Height; 
関連する問題