On the internet, there are lot of code snippets for fitting an image to a picture box. Many of those involve operations on the bitmap, resize and interpolate.
But that can be easily achieved by transforming (scaling) the device context (Graphics object in C#) and draw the bitmap over the transformed device context.
Here it stores image in a global variable. Then this image is used to redraw in the picture box. The image is finally disposed at the disposed event of the form. No operations on the bitmap is needed.
For testing this code, you need a form with a picture box (
pictureBox1
), a Zoom button(
button1
), a text box (
textBox1
) to enter the zoom factor and a move button (
button2
).
Then use the code below.
Initially it fits the image to the
picturebox
(Zoom factor 1).
To Zoom, enter the zoom factor and click the zoom button.
To move the image, click the move button, click on the image, keep hold the clicked mouse button and drag. Release the mouse button.
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private float zoomFac = 1;
private bool zoomSet = false;
private float translateX=0;
private float translateY = 0;
private bool translateSet = false;
private bool translate = false;
private float transStartX;
private float transStartY;
private float curImageX=0;
private float curImageY=0;
Image bmp;
float ratio;
float translateRatio;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.Paint += new PaintEventHandler(pictureBox1_Paint);
pictureBox1.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);
pictureBox1.MouseUp += new MouseEventHandler(pictureBox1_MouseUp);
pictureBox1.BorderStyle = BorderStyle.FixedSingle;
if (System.IO.File.Exists(Application.StartupPath + "//test.jpg") != true)
{
MessageBox.Show("Your Image file does not exists");
return;
}
if (bmp != null)
{
bmp.Dispose();
}
bmp = new Bitmap(Application.StartupPath + "//test.jpg");
if (bmp.Width > bmp.Height)
{
ratio = (float)pictureBox1.Width / (float)bmp.Width;
translateRatio = (float)bmp.Width / (float)pictureBox1.Width;
}
else
{
ratio = (float)pictureBox1.Height / (float)bmp.Height;
translateRatio = (float)bmp.Height / (float)pictureBox1.Height;
}
this.Shown +=new EventHandler(Form1_Shown);
this.Disposed +=new EventHandler(Form1_Disposed);
}
protected void Form1_Disposed(object sender, EventArgs e)
{
if (bmp != null)
{
bmp.Dispose();
}
}
protected void Form1_Shown(object sender, EventArgs e)
{
zoomSet = true;
pictureBox1.Refresh();
zoomSet = false;
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || Single.TryParse(textBox1.Text, out zoomFac)==false) { return; }
zoomSet = true;
pictureBox1.Refresh();
pictureBox1.Cursor = Cursors.Arrow;
translateSet = false;
}
private void button2_Click(object sender, EventArgs e)
{
pictureBox1.Cursor = Cursors.Hand;
translateSet = true;
zoomSet = false;
}
protected void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (translateSet == true)
{
translate = true;
transStartX = e.X;
transStartY = e.Y;
}
}
protected void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (translate == true)
{
translateX = curImageX + ((e.X - transStartX) * (translateRatio/zoomFac));
translateY = curImageY + ((e.Y - transStartY) * (translateRatio/zoomFac));
}
pictureBox1.Refresh();
translate = false;
curImageX = translateX;
curImageY = translateY;
}
protected void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (bmp == null) { return; }
if (translateSet == false && zoomSet == false) { return; }
Graphics g = e.Graphics;
g.ScaleTransform(ratio * zoomFac, ratio * zoomFac);
if (translateSet == true)
{
g.TranslateTransform(translateX, translateY);
}
g.DrawImage(bmp, 0, 0);
}
}
}
[change history]
Minor error checking has been done.
Mouse move event is removed to increase performance and that operations moved to mouse up event
[/change history]