none
Bitmap Resolution with Crystal Image+C#

    Question

  • How to get the accurate Bitmap Graphics In C# after zooming on MouseWheel.

    I already used the InterpolationModeHighQuality.Bicubic &
    InterpolationModeHighQuality.Bilinear. But there is no change in Bitmap resolution.

    Whenever I draw a Bitmap on Mouseclick the Image resolution also changed.

    How to rectify this task.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.Serialization;


    namespace WindowsApplication10
    {
        public partial class Form1 : Form
        {
            public static Bitmap bmp1;
            public Bitmap bmp;
            public Graphics grfx;
            public  float zoom = 1;
            public Form1()
            {
                grfx=this.CreateGraphics();
                grfx.Clear(this.BackColor);
                bmp = new Bitmap(2500, 2500);
                grfx = Graphics.FromImage(bmp);
                grfx.InterpolationMode = InterpolationMode.High;
                Zoom = 1F;
                this.DoubleBuffered = true;
                InitializeComponent();
               
            }
            protected override void OnPaint(PaintEventArgs e)
            {
                Graphics g = e.Graphics;
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.DrawImage(bmp, this.AutoScrollPosition.X, this.AutoScrollPosition.Y, this.AutoScrollMinSize.Width, this.AutoScrollMinSize.Height);
            }


            private void Form1_MouseClick(object sender, MouseEventArgs e)
            {

                Graphics g = this.CreateGraphics();
                Pen p = new Pen(Color.Blue);
                Matrix x = new Matrix();
                x.Scale(Zoom, Zoom);
                g.Transform = x;

                GraphicsPath path = new GraphicsPath();
                path.AddEllipse(e.X / Zoom, e.Y / Zoom, 50, 50);
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                g.InterpolationMode = InterpolationMode.High;
                g.InterpolationMode = InterpolationMode.HighQualityBilinear;
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.DrawPath(p, path);

                grfx.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                grfx.InterpolationMode = InterpolationMode.High;
                grfx.InterpolationMode = InterpolationMode.HighQualityBilinear;
                grfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
                grfx.DrawPath(p, path);

            }


            protected override void OnMouseWheel(MouseEventArgs e)
            {
                if (e.Delta > 0)
                {

                    if (Zoom < 10.1F) Zoom += 0.1F;

                }

                else if (e.Delta < 0)
                {

                    if (Zoom > 0.1F) Zoom -= 0.1F;
                }

            }
           
     
            public float Zoom
            {
                get
                {
                    return zoom;
                }
                set
                {
                    zoom = value;
                    UpdateControl();
                }
            }
            float w = 0;
            float h = 0;
            void UpdateControl()
            {

                    w =  Zoom * (float)bmp.Width;
                    h =  Zoom * (float)bmp.Height;
                    this.AutoScrollMinSize = new Size((int)w, (int)h);
                    this.Invalidate();
            }
        }
    }


    Cheers & Regards
    C.Vengat


    Wednesday, May 14, 2008 6:10 AM

Answers

  • Hi vengat,

    I have done test your code in my sample, indeed, it works well without any question, the resolution still kept on mouse clicking event,  and I added my code to get the zoomed bitmap in the onpaint event handler, please check the code snippet below.

    Code Snippet

    //File: Form1.cs

    namespace Zoom_Bitmap_Demo

    {

        public partial class Form1 : Form

        {    

            public Bitmap bmp;

            public Graphics grfx;

            public float zoom = 1;

     

            public Form1()

            {

                grfx = this.CreateGraphics();

                grfx.Clear(this.BackColor);

                //bmp = new Bitmap(2500, 2500);

                bmp = (Bitmap)(Bitmap.FromFile("F:\\TestPicture.jpg"));

                grfx = Graphics.FromImage(bmp);

                grfx.InterpolationMode = InterpolationMode.High;

                Zoom = 1F;

                this.DoubleBuffered = true;

     

                InitializeComponent();

            }

     

            private void Form1_Paint(object sender, PaintEventArgs e)

            {// Hi, member variable bmp is the orignal bitmap

             // the local variable tmpBitmap is the zoomed bitmap from bmp

     

                Graphics g = e.Graphics;

                g.InterpolationMode = InterpolationMode.HighQualityBicubic;

                Bitmap tmpBitmap = new Bitmap(bmp, this.AutoScrollMinSize.Width, this.AutoScrollMinSize.Height);

               int width = tmpBitmap.Width;

                int height = tmpBitmap.Height;

     

                g.DrawImage(tmpBitmap, this.AutoScrollPosition.X, this.AutoScrollPosition.Y, width, height);

     

            }

     

            private void Form1_MouseClick(object sender, MouseEventArgs e)

            {

                Graphics g = this.CreateGraphics();

                Pen p = new Pen(Color.Blue);

                Matrix x = new Matrix();

                x.Scale(Zoom, Zoom);

                g.Transform = x;

     

                GraphicsPath path = new GraphicsPath();

                path.AddEllipse(e.X / Zoom, e.Y / Zoom, 50, 50);

                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

                g.InterpolationMode = InterpolationMode.High;

                g.InterpolationMode = InterpolationMode.HighQualityBilinear;

                g.InterpolationMode = InterpolationMode.HighQualityBicubic;

                g.DrawPath(p, path);

     

                grfx.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

                grfx.InterpolationMode = InterpolationMode.High;

                grfx.InterpolationMode = InterpolationMode.HighQualityBilinear;

                grfx.InterpolationMode = InterpolationMode.HighQualityBicubic;

                grfx.DrawPath(p, path);

     

            }

     

            protected override void OnMouseWheel(MouseEventArgs e)

            {

                if (e.Delta > 0)

                {

                    if (Zoom < 10.1F) Zoom += 0.1F;

                }

                else if (e.Delta < 0)

                {

                    if (Zoom > 0.1F) Zoom -= 0.1F;

                }

            }

     

     

            public float Zoom

            {

                get

                {

                    return zoom;

                }

                set

                {

                    zoom = value;

                    UpdateControl();

                }

            }

            float w = 0;

            float h = 0;

            void UpdateControl()

            {

     

                w = Zoom * (float)bmp.Width;

                h = Zoom * (float)bmp.Height;

                this.AutoScrollMinSize = new Size((int)w, (int)h);

                this.Invalidate();

            }

     

        }

    }

     

     

     

     

    Friday, May 16, 2008 8:50 AM
  • Hi vengat,

    The blurred effect was yielded by the zoom algorithm in the .Net framework, I think to re-draw the graphic is the sole approach, I have written the code snippet to implement this function, please check the code snippet below, please notice the italic code.

    Code Snippet

    public partial class Form1 : Form

        {

            public Bitmap bmp;

            public Graphics grfx;

            public float zoom = 1;

            ArrayList _ellipsesArray = new ArrayList();

            public Form1()

            {

               

                grfx = this.CreateGraphics();

                grfx.Clear(this.BackColor);

                //bmp = new Bitmap(2500, 2500);

                bmp = (Bitmap)(Bitmap.FromFile("F:\\TestPicture.jpg"));

                grfx = Graphics.FromImage(bmp);

                grfx.InterpolationMode = InterpolationMode.High;

                Zoom = 1F;

                this.DoubleBuffered = true;

     

                InitializeComponent();           

            }

     

            private void Form1_Paint(object sender, PaintEventArgs e)

            {// Hi, member variable bmp is the orignal bitmap

             // the local variable tmpBitmap is the zoomed bitmap from bmp

     

                Graphics g = e.Graphics;

                g.InterpolationMode = InterpolationMode.HighQualityBicubic;

                Bitmap tmpBitmap = new Bitmap(bmp, this.AutoScrollMinSize.Width, this.AutoScrollMinSize.Height);

                int width = tmpBitmap.Width;

                int height = tmpBitmap.Height;

     

                g.DrawImage(tmpBitmap, this.AutoScrollPosition.X, this.AutoScrollPosition.Y, this.AutoScrollMinSize.Width, this.AutoScrollMinSize.Height);

     

                Pen p = new Pen(Color.Blue);

                foreach (Point p1 in _ellipsesArray)

                    g.DrawEllipse(p, p1.X * Zoom, p1.Y * Zoom, 50 * Zoom, 50 * Zoom);

     

            }

     

            private void Form1_MouseClick(object sender, MouseEventArgs e)

            {

     

                _ellipsesArray.Add(new Point(e.X, e.Y));

                

                Graphics g = this.CreateGraphics();

                Pen p = new Pen(Color.Blue);

                Matrix x = new Matrix();

                x.Scale(Zoom, Zoom);

                g.Transform = x;

                g.DrawEllipse(p,e.X / Zoom, e.Y / Zoom, 50, 50);

                

                return;           

            }

             // the other code was not changed

     

     

    Regards,

    Xun

     

    Tuesday, May 20, 2008 7:37 AM

All replies

  • Hi vengat,

    I have done test your code in my sample, indeed, it works well without any question, the resolution still kept on mouse clicking event,  and I added my code to get the zoomed bitmap in the onpaint event handler, please check the code snippet below.

    Code Snippet

    //File: Form1.cs

    namespace Zoom_Bitmap_Demo

    {

        public partial class Form1 : Form

        {    

            public Bitmap bmp;

            public Graphics grfx;

            public float zoom = 1;

     

            public Form1()

            {

                grfx = this.CreateGraphics();

                grfx.Clear(this.BackColor);

                //bmp = new Bitmap(2500, 2500);

                bmp = (Bitmap)(Bitmap.FromFile("F:\\TestPicture.jpg"));

                grfx = Graphics.FromImage(bmp);

                grfx.InterpolationMode = InterpolationMode.High;

                Zoom = 1F;

                this.DoubleBuffered = true;

     

                InitializeComponent();

            }

     

            private void Form1_Paint(object sender, PaintEventArgs e)

            {// Hi, member variable bmp is the orignal bitmap

             // the local variable tmpBitmap is the zoomed bitmap from bmp

     

                Graphics g = e.Graphics;

                g.InterpolationMode = InterpolationMode.HighQualityBicubic;

                Bitmap tmpBitmap = new Bitmap(bmp, this.AutoScrollMinSize.Width, this.AutoScrollMinSize.Height);

               int width = tmpBitmap.Width;

                int height = tmpBitmap.Height;

     

                g.DrawImage(tmpBitmap, this.AutoScrollPosition.X, this.AutoScrollPosition.Y, width, height);

     

            }

     

            private void Form1_MouseClick(object sender, MouseEventArgs e)

            {

                Graphics g = this.CreateGraphics();

                Pen p = new Pen(Color.Blue);

                Matrix x = new Matrix();

                x.Scale(Zoom, Zoom);

                g.Transform = x;

     

                GraphicsPath path = new GraphicsPath();

                path.AddEllipse(e.X / Zoom, e.Y / Zoom, 50, 50);

                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

                g.InterpolationMode = InterpolationMode.High;

                g.InterpolationMode = InterpolationMode.HighQualityBilinear;

                g.InterpolationMode = InterpolationMode.HighQualityBicubic;

                g.DrawPath(p, path);

     

                grfx.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

                grfx.InterpolationMode = InterpolationMode.High;

                grfx.InterpolationMode = InterpolationMode.HighQualityBilinear;

                grfx.InterpolationMode = InterpolationMode.HighQualityBicubic;

                grfx.DrawPath(p, path);

     

            }

     

            protected override void OnMouseWheel(MouseEventArgs e)

            {

                if (e.Delta > 0)

                {

                    if (Zoom < 10.1F) Zoom += 0.1F;

                }

                else if (e.Delta < 0)

                {

                    if (Zoom > 0.1F) Zoom -= 0.1F;

                }

            }

     

     

            public float Zoom

            {

                get

                {

                    return zoom;

                }

                set

                {

                    zoom = value;

                    UpdateControl();

                }

            }

            float w = 0;

            float h = 0;

            void UpdateControl()

            {

     

                w = Zoom * (float)bmp.Width;

                h = Zoom * (float)bmp.Height;

                this.AutoScrollMinSize = new Size((int)w, (int)h);

                this.Invalidate();

            }

     

        }

    }

     

     

     

     

    Friday, May 16, 2008 8:50 AM
  • And the InitializeComponent method is below.

    Code Snippet

    private void InitializeComponent()

            {

                this.SuspendLayout();

                //

                // Form1

                //

                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);

                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

                this.ClientSize = new System.Drawing.Size(292, 273);

                this.Name = "Form1";

                this.Text = "Form1";

                this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

                this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseClick);

                this.ResumeLayout(false); 

            }

     

    Regards,

    Xun

     

    Friday, May 16, 2008 8:51 AM
  • Please find the url



    http://www.screenshots.cc/show.php/17886_obtainedimage.JPG.html


                    In which u can notice the blurred effect of the bitmap(raster image)....while zoom level...Please give me the solution to solve the blurred effect in image(1)......
    Monday, May 19, 2008 8:22 AM
  • Hi vengat ,


     I think the blurred effect was caused by the circle graphic was enlarged from the small one, to fix this issue; you can try to re-draw the circle graphic with the coincident proportion.


    Regards,

    Xun

    Monday, May 19, 2008 10:42 AM
  • Hi Xun,

    Thanks for your quick reply, Is there is any other solution please help me.


    Regards
    C.Vengat
    Tuesday, May 20, 2008 4:44 AM
  • Hi vengat,

    The blurred effect was yielded by the zoom algorithm in the .Net framework, I think to re-draw the graphic is the sole approach, I have written the code snippet to implement this function, please check the code snippet below, please notice the italic code.

    Code Snippet

    public partial class Form1 : Form

        {

            public Bitmap bmp;

            public Graphics grfx;

            public float zoom = 1;

            ArrayList _ellipsesArray = new ArrayList();

            public Form1()

            {

               

                grfx = this.CreateGraphics();

                grfx.Clear(this.BackColor);

                //bmp = new Bitmap(2500, 2500);

                bmp = (Bitmap)(Bitmap.FromFile("F:\\TestPicture.jpg"));

                grfx = Graphics.FromImage(bmp);

                grfx.InterpolationMode = InterpolationMode.High;

                Zoom = 1F;

                this.DoubleBuffered = true;

     

                InitializeComponent();           

            }

     

            private void Form1_Paint(object sender, PaintEventArgs e)

            {// Hi, member variable bmp is the orignal bitmap

             // the local variable tmpBitmap is the zoomed bitmap from bmp

     

                Graphics g = e.Graphics;

                g.InterpolationMode = InterpolationMode.HighQualityBicubic;

                Bitmap tmpBitmap = new Bitmap(bmp, this.AutoScrollMinSize.Width, this.AutoScrollMinSize.Height);

                int width = tmpBitmap.Width;

                int height = tmpBitmap.Height;

     

                g.DrawImage(tmpBitmap, this.AutoScrollPosition.X, this.AutoScrollPosition.Y, this.AutoScrollMinSize.Width, this.AutoScrollMinSize.Height);

     

                Pen p = new Pen(Color.Blue);

                foreach (Point p1 in _ellipsesArray)

                    g.DrawEllipse(p, p1.X * Zoom, p1.Y * Zoom, 50 * Zoom, 50 * Zoom);

     

            }

     

            private void Form1_MouseClick(object sender, MouseEventArgs e)

            {

     

                _ellipsesArray.Add(new Point(e.X, e.Y));

                

                Graphics g = this.CreateGraphics();

                Pen p = new Pen(Color.Blue);

                Matrix x = new Matrix();

                x.Scale(Zoom, Zoom);

                g.Transform = x;

                g.DrawEllipse(p,e.X / Zoom, e.Y / Zoom, 50, 50);

                

                return;           

            }

             // the other code was not changed

     

     

    Regards,

    Xun

     

    Tuesday, May 20, 2008 7:37 AM

  •  hi Xun Ye,
            I too facing the same problem .. your code is working nice but It takes more time(performance issue) since in this code I think you are  re-drawing  in ONPAINT ..Is there is any other solution to overcome it..

    CHEERS & Regards.
    ALGATES.
    Tuesday, May 27, 2008 10:16 AM