Thứ Ba, 23 tháng 3, 2010

// // Leave a Comment

Resizing Width of Picturebox C#

Ví dụ dưới đây sử dụng scroll bar để tùy chỉnh cách hiển thị của picturebox cho vừa kích thước của bức ảnh đưa vào.



namespace Scrollbar
{
public class Form1 : Form
{
private OpenFileDialog openFileDialog1;
private PictureBox pictureBox1;
private VScrollBar vScrollBar1;
private HScrollBar hScrollBar1;

public Form1()
{
InitializeComponent();
}

private void InitializeComponent()
{
this.openFileDialog1 = new OpenFileDialog();
this.pictureBox1 = new PictureBox();
this.vScrollBar1 = new VScrollBar();
this.hScrollBar1 = new HScrollBar();
this.pictureBox1.SuspendLayout();
this.SuspendLayout();

this.pictureBox1.BorderStyle = BorderStyle.FixedSingle;
this.pictureBox1.Controls.AddRange(
new Control[] { this.vScrollBar1, this.hScrollBar1});
this.pictureBox1.Dock = DockStyle.Fill;
this.pictureBox1.Size = new System.Drawing.Size(440, 349);
this.pictureBox1.DoubleClick += new EventHandler(this.pictureBox1_DoubleClick);


this.vScrollBar1.Dock = DockStyle.Right;
this.vScrollBar1.Location = new System.Drawing.Point(422, 0);
this.vScrollBar1.Size = new System.Drawing.Size(16, 331);
this.vScrollBar1.Visible = false;
this.vScrollBar1.Scroll +=
new ScrollEventHandler(this.HandleScroll);

this.hScrollBar1.Dock = DockStyle.Bottom;
this.hScrollBar1.Location = new System.Drawing.Point(0, 331);
this.hScrollBar1.Size = new System.Drawing.Size(438, 16);
this.hScrollBar1.Visible = false;
this.hScrollBar1.Scroll +=
new ScrollEventHandler(this.HandleScroll);

this.ClientSize = new System.Drawing.Size(440, 349);
this.Controls.AddRange(new Control[] {this.pictureBox1});
this.Text = "Form1";
this.Resize += new System.EventHandler(this.Form1_Resize);
this.pictureBox1.ResumeLayout(false);
this.ResumeLayout(false);


}


[STAThread]
static void Main()
{
Application.Run(new Form1());
}


private void pictureBox1_DoubleClick (Object sender, EventArgs e)
{
// Mở một bức ảnh.
if(openFileDialog1.ShowDialog() != DialogResult.Cancel)
{
// Đưa ảnh vào picturebox
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
this.DisplayScrollBars();
this.SetScrollBarValues();
}
}

private void Form1_Resize (Object sender, EventArgs e)
{
// If the PictureBox has an image, see if it needs
// scrollbars and refresh the image.
if(pictureBox1.Image != null)
{
this.DisplayScrollBars();
this.SetScrollBarValues();
this.Refresh();
}
}

public void DisplayScrollBars()
{
// Nếu ảnh đưa vào có độ rộng hơn picturebox thì cho hiển thị HScrollBar.
if (pictureBox1.Width > pictureBox1.Image.Width - this.vScrollBar1.Width)
{
hScrollBar1.Visible = false;
}
else
{
hScrollBar1.Visible = true;
}

// Ảnh đưa vào cao hơn kích thước của PictureBox thì cho hiển thị VScrollBar.
if (pictureBox1.Height >
pictureBox1.Image.Height - this.hScrollBar1.Height)
{
vScrollBar1.Visible = false;
}
else
{
vScrollBar1.Visible = true;
}
}

private void HandleScroll(Object sender, ScrollEventArgs se)
{
/* Khởi tạo một đối tượng Graphics và vẽ lại một phần của bức ảnh lên PictureBox. */
Graphics g = pictureBox1.CreateGraphics();

g.DrawImage(pictureBox1.Image,
new Rectangle(0, 0, pictureBox1.Right - vScrollBar1.Width,
pictureBox1.Bottom - hScrollBar1.Height),
new Rectangle(hScrollBar1.Value, vScrollBar1.Value,
pictureBox1.Right - vScrollBar1.Width,
pictureBox1.Bottom - hScrollBar1.Height),
GraphicsUnit.Pixel);


pictureBox1.Update();
}


public void SetScrollBarValues()
{
// Thiết lập thuộc tính Maximum, Minimum, LargeChange và SmallChange.
this.vScrollBar1.Minimum = 0;
this.hScrollBar1.Minimum = 0;


// If the offset does not make the Maximum less than zero, set its value.
if( (this.pictureBox1.Image.Size.Width - pictureBox1.ClientSize.Width) > 0)
{
this.hScrollBar1.Maximum =
this.pictureBox1.Image.Size.Width - pictureBox1.ClientSize.Width;
}

   // Nếu HScrollBar được hiển thị, thì điều chỉnh giá trị Max của
// hSCrollBar để thêm vào độ rộng của vScroll

if(this.vScrollBar1.Visible)
{
this.hScrollBar1.Maximum += this.vScrollBar1.Width;
}
this.hScrollBar1.LargeChange = this.hScrollBar1.Maximum / 10;
this.hScrollBar1.SmallChange = this.hScrollBar1.Maximum / 20;


// Điều chỉnh giá trị Max nguyên từ tương tác người dùng.
this.hScrollBar1.Maximum += this.hScrollBar1.LargeChange;

// If the offset does not make the Maximum less than zero, set its value.
if( (this.pictureBox1.Image.Size.Height - pictureBox1.ClientSize.Height) > 0)
{
this.vScrollBar1.Maximum =
this.pictureBox1.Image.Size.Height - pictureBox1.ClientSize.Height;
}


// Nếu HScrollBar được hiển thị, thì điều chỉnh giá trị Max của
// VSCrollBar để thêm vào độ rộng của HScrollbar
if(this.hScrollBar1.Visible)
{
this.vScrollBar1.Maximum += this.hScrollBar1.Height;
}
this.vScrollBar1.LargeChange = this.vScrollBar1.Maximum / 10;
this.vScrollBar1.SmallChange = this.vScrollBar1.Maximum / 20;

// Điều chỉnh giá trị Max nguyên có được từ tương tác người dùng.
this.vScrollBar1.Maximum += this.vScrollBar1.LargeChange;
}


}
}

0 comments: