Zooming Using Matrices....
-
Hi, I'm quite new to C# especially when using GDI+. I saw the solution for zooming an image, but I didn't manage to zoom. I couldn't quite understand what the parameters in "new Matrix(scale,0,0,scale, xoffest, yoffset);" mean I think In order to zoom my image by 25% I implemented your code in the following way: float scale = 25f/100f; pictureBox.Image.Save("OriginalImage.png",ImageFormat.Png); Graphics g = Graphics.FromImage(pictureBox.Image); g.Transform = new Matrix(scale,0,0,scale, 0, 0); pictureBox.Image.Save("ResizedImage.png", ImageFormat.Png); pictureBox.Image = Image.FromFile("ResizedImage.png"); //to load the new image What am I doing wrong please? Thanks A Lot! C.N.
-
Hi, I'm quite new to C# especially when using GDI+. I saw the solution for zooming an image, but I didn't manage to zoom. I couldn't quite understand what the parameters in "new Matrix(scale,0,0,scale, xoffest, yoffset);" mean I think In order to zoom my image by 25% I implemented your code in the following way: float scale = 25f/100f; pictureBox.Image.Save("OriginalImage.png",ImageFormat.Png); Graphics g = Graphics.FromImage(pictureBox.Image); g.Transform = new Matrix(scale,0,0,scale, 0, 0); pictureBox.Image.Save("ResizedImage.png", ImageFormat.Png); pictureBox.Image = Image.FromFile("ResizedImage.png"); //to load the new image What am I doing wrong please? Thanks A Lot! C.N.
You are scaling the System.Drawing.Graphics object. But that has no effect on the actual System.Drawing.Image object in the PictureBox.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: The Secular Left, the Religious Right, and Prejudice Judah Himango
-
You are scaling the System.Drawing.Graphics object. But that has no effect on the actual System.Drawing.Image object in the PictureBox.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: The Secular Left, the Religious Right, and Prejudice Judah Himango
-
Hi, can you kindly give me an example of how can I zoom an image in a PictureBox using the Matrix method (as I had shown in the code-snippet) or any other method please? Thanks C.N.
You are not zooming the image itself. Instead, you are telling the view (Graphics) how to render the image. This is the advantage of using Transforms... drawing code does not change. Just the parameters of the rendering. Derive a class from PictureBox (or control type of choice...) and modify the OnPaint routine appropriately. public class ZoomPictureBox : PictureBox { private float m_zoom = 1.0F; public float Zoom { get{ return m_zoom; } set{ m_zoom = value; if( this.Image != null ) Invalidate(); } protected override void OnPaint(PaintEventArgs e) { Matrix matrix = new Matrix(); matrix.Scale( m_zoom, m_zoom ); e.Graphics.Transform = matrix; e.Graphics.DrawImageUnscaled( this.Image, 0, 0 ); // base.OnPaint (e); } }