C# .NET - how to convert small image into big image (zoom)?

Asked By Prem Anandh on 29-Oct-08 06:45 AM
i am use some photos in my website..i has displying image on label (small images) if now i click that images then image will display zoom(big size compare from normal size)..is it possbile?


Use this

Kalit Sikka replied to Prem Anandh on 29-Oct-08 07:04 AM
Enter your C# source code below and click submit to color code.
public Image PictureBoxZoom(Image img, Size size)
{
    Bitmap bm = new Bitmap(img, Convert.ToInt32(img.Width * size.Width), Convert.ToInt32(img.Height * size.Height));
    Graphics grap = Graphics.FromImage(bm);
    grap.InterpolationMode = InterpolationMode.HighQualityBicubic; 
    return bm;
}
In the code above, we create a new instance of the Bitmap class using the image passed to the function. This image is scaled to the specific size and is taken into a Graphics object where the image is prefiltered using InterpolationMode.HighQualityBicubic to ensure high quality transformed images. The function then returns the transformed image back to the caller.

Reply

Binny ch replied to Prem Anandh on 29-Oct-08 07:56 AM
 using System.Web.UI.WebControls;
 using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 public partial class Simple : System.Web.UI.Page
 {
     private SharpMap.Map myMap;
 
     protected void Page_Load(object sender, EventArgs e)
    {
         //Set up the map. We use the method in the App_Code folder for initializing the map
         myMap = MapHelper.InitializeMap(new System.Drawing.Size((int)imgMap.Width.Value,(int)i  mgMap.Height.Value));
       if (Page.IsPostBack)
        {
            //Page is post back. Restore center and zoom-values from viewstate
             myMap.Center = (SharpMap.Geometries.Point)ViewState["mapCenter"];
            myMap.Zoom = (double)ViewState["mapZoom"];
         }
        else
        {
            //This is the initial view of the map. Zoom to the extents of the map:
            //myMap.ZoomToExtents();
             //or center on 0,0 and zoom to full earth (360 degrees)
             //myMap.Center = new SharpMap.Geometries.Point(0,0);
            //myMap.Zoom = 360;
             //Create the map
            GenerateMap();
     }
 
    protected void imgMap_Click(object sender, ImageClickEventArgs e)
     {
         //Set center of the map to where the client clicked
        myMap.Center = myMap.ImageToWorld(new System.Drawing.Point(e.X, e.Y));
         //Set zoom value if any of the zoom tools were selected
         if (rblMapTools.SelectedValue == "0") //Zoom in
             myMap.Zoom = myMap.Zoom * 0.5;
          if (rblMapTools.SelectedValue == "1") //Zoom out
             myMap.Zoom = myMap.Zoom * 2;
         else if (rblMapTools.SelectedValue == "3") // Zoom Extent
             myMap.ZoomToExtents();
 
         //Create the map
         GenerateMap();
     }
 
     /// <summary>
     /// Creates the map, inserts it into the cache and sets the ImageButton Url
     /// </summary>
     private void GenerateMap()
     {
         //Save the current mapcenter and zoom in the viewstate
         ViewState.Add("mapCenter", myMap.Center);
         ViewState.Add("mapZoom", myMap.Zoom);
         //Render map
         System.Drawing.Image img = myMap.GetMap();
         string imgID = SharpMap.Web.Caching.InsertIntoCache(1, img);
         imgMap.ImageUrl = "getmap.aspx?ID=" + HttpUtility.UrlEncode(imgID);
     }
 
 

Solution

san san replied to Prem Anandh on 29-Oct-08 12:38 PM
Hi

Try the code bellow to zoom an image on a label...

 [STAThread]
        static void Main()
        {
            Application.Run(new ImageZoom1());
        }
        protected override void OnPaint(PaintEventArgs e)
        {   ImageZoom();    }

     
         protected void ImageZoom()
        {
            Graphics g1 = Graphics.FromHwnd(this.label1.Handle);
            Graphics g2 = Graphics.FromHwnd(this.label2.Handle);
            Rectangle rec;
            Rectangle recPart;

            if (this.checkBox1.Checked)
            {
                if (im == null) ReadImage();
       
                rec = new Rectangle(0, 0, label1.Width, label1.Height);
                g1.DrawImage(im, rec);
              
                // Center part:
                recPart = new Rectangle(im.Width/4, im.Height/4, im.Width/2,
                    im.Height/2);
                if(this.radioButton1.Checked)  // Left-Top part
                    recPart = new Rectangle(0, 0, im.Width/2, im.Height/2);
                if(this.radioButton2.Checked)  // Right-Top part
                    recPart = new Rectangle(im.Width/2, 0, im.Width/2, im.Height/2);
                if(this.radioButton3.Checked)  // Left-Down part
                    recPart = new Rectangle(0, im.Height/2, im.Width/2, im.Height/2);
                if(this.radioButton4.Checked)  // Right-Down part
                    recPart = new Rectangle(im.Width/2, im.Height/2, im.Width/2,
                        im.Height/2);

                g2.DrawImage(im, rec, recPart, GraphicsUnit.Pixel);
            }
            else
            {
                Clear(g1);
                Clear(g2);
            }

            g1.Dispose();   g2.Dispose();    
        }

        protected void ReadImage()
        {
            string path = @"image.BMP";
            im = Image.FromFile(path);
           
        }

        protected void Clear(Graphics g)
        {
            g.Clear(this.BackColor);
            g.Dispose();      
            im = null;
            im2 = null;
         
        }
    }
}