This sample code ssnippet shows how to zoom a page using C#.
public partial class Form1 : Form { Rectangle rubberBand,zoom_Rect; Bitmap bmp,bmp_cut; float m_Scalef = 1.0f; Image image,myImage;
public Form1() { InitializeComponent(); bmp = (Bitmap)pictureBox1.Image;
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { Point pt = pictureBox1.PointToScreen(new Point(e.X, e.Y)); rubberBand = new Rectangle(pt.X, pt.Y, 1, 1); ControlPaint.DrawReversibleFrame(rubberBand, pictureBox1.BackColor, FrameStyle.Dashed); this.Invalidate(); }
private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { if (e.Button != MouseButtons.Left) return;
ControlPaint.DrawReversibleFrame(rubberBand, pictureBox1.BackColor, FrameStyle.Dashed);
Point pt = pictureBox1.PointToScreen(new Point(e.X, e.Y)); rubberBand = new Rectangle(rubberBand.X, rubberBand.Y, pt.X - rubberBand.X, pt.Y - rubberBand.Y);
ControlPaint.DrawReversibleFrame(rubberBand, pictureBox1.BackColor, FrameStyle.Dashed);
pictureBox1.Invalidate();
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { ControlPaint.DrawReversibleFrame(rubberBand, Color.Black, FrameStyle.Thick); }
}
private void button2_Click(object sender, EventArgs e) { zoom_Rect = pictureBox1.RectangleToClient(rubberBand); bmp_cut = new Bitmap(zoom_Rect.Width, zoom_Rect.Height);
Graphics g = Graphics.FromImage(bmp_cut); image = pictureBox1.Image; g.DrawImage(image, 0, 0, zoom_Rect, GraphicsUnit.Pixel); pictureBox1.Image = bmp_cut; g.Dispose();
}
private void button3_Click(object sender, EventArgs e) {
panel1.Refresh(); if (comboBox1.SelectedIndex == 0) m_Scalef = .25f; if (comboBox1.SelectedIndex == 1) m_Scalef = .5f; if (comboBox1.SelectedIndex == 2) m_Scalef = .75f; if (comboBox1.SelectedIndex == 3) m_Scalef = 1.0f; if (comboBox1.SelectedIndex == 4) m_Scalef = 2.0f;
m_Scalef = m_Scalef * 2.0f; int a_Width, b_Height; a_Width = (int)(rubberBand.Width * m_Scalef); b_Height = (int)(rubberBand.Height * m_Scalef); panel1.Size = new Size(a_Width, b_Height);
myImage = pictureBox1.Image; Graphics g = panel1.CreateGraphics(); g.DrawImage(myImage, 0, 0, rubberBand.Width * m_Scalef, rubberBand.Height * m_Scalef);
Invalidate(); } }
It really helps in basic zooming concept,so any one can make use of this code...:)
|
No responses found. Be the first to respond and make money from revenue sharing program.
|