Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. HOw to resize Images in C#/Asp.NET

HOw to resize Images in C#/Asp.NET

Scheduled Pinned Locked Moved C#
graphicscsharpasp-netsysadmintutorial
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Software2007
    wrote on last edited by
    #1

    -I am trying to learn how to load up a Gallery of images to asp website.I understand I could load thumbnail images to reduce original size. Here is some code I tried after googling, but its still loading original size. -Also, Can I make these images look more alive, like clikable, or double their size whenclicked?

    protected void btnsave_Click(object sender, EventArgs e)
    {
    string filename = Path.GetFileName(fileupload1.PostedFile.FileName);
    string targetPath = Server.MapPath("Images/" + filename);
    Stream strm = fileupload1.PostedFile.InputStream;
    var targetFile = targetPath;
    //Based on scalefactor image size will vary
    GenerateThumbnails(0.5, strm, targetFile);
    BindDataList();
    }

    private void GenerateThumbnails(double scaleFactor, Stream sourcePath,string targetPath)
    {
    using (var image = System.Drawing.Image.FromStream(sourcePath))
    {
    // can given width of image as we want
    var newWidth = (int)(image.Width * scaleFactor);
    // can given height of image as we want
    var newHeight = (int)(image.Height * scaleFactor);
    var thumbnailImg = new Bitmap(newWidth, newHeight);
    var thumbGraph = Graphics.FromImage(thumbnailImg);

                thumbGraph.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                thumbGraph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                thumbGraph.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
                thumbGraph.DrawImage(image, imageRectangle);
                thumbnailImg.Save(targetPath, image.RawFormat);
            }
        }
    

    protected void BindDataList()
    {
    DirectoryInfo dir = new DirectoryInfo(MapPath("~/Images"));
    FileInfo[] files = dir.GetFiles();
    ArrayList listItems = new ArrayList();
    foreach (FileInfo info in files)
    {
    listItems.Add(info);
    }
    dtlist.DataSource = listItems;
    dtlist.DataBind();
    }

    S S B 4 Replies Last reply
    0
    • S Software2007

      -I am trying to learn how to load up a Gallery of images to asp website.I understand I could load thumbnail images to reduce original size. Here is some code I tried after googling, but its still loading original size. -Also, Can I make these images look more alive, like clikable, or double their size whenclicked?

      protected void btnsave_Click(object sender, EventArgs e)
      {
      string filename = Path.GetFileName(fileupload1.PostedFile.FileName);
      string targetPath = Server.MapPath("Images/" + filename);
      Stream strm = fileupload1.PostedFile.InputStream;
      var targetFile = targetPath;
      //Based on scalefactor image size will vary
      GenerateThumbnails(0.5, strm, targetFile);
      BindDataList();
      }

      private void GenerateThumbnails(double scaleFactor, Stream sourcePath,string targetPath)
      {
      using (var image = System.Drawing.Image.FromStream(sourcePath))
      {
      // can given width of image as we want
      var newWidth = (int)(image.Width * scaleFactor);
      // can given height of image as we want
      var newHeight = (int)(image.Height * scaleFactor);
      var thumbnailImg = new Bitmap(newWidth, newHeight);
      var thumbGraph = Graphics.FromImage(thumbnailImg);

                  thumbGraph.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                  thumbGraph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                  thumbGraph.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                  var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
                  thumbGraph.DrawImage(image, imageRectangle);
                  thumbnailImg.Save(targetPath, image.RawFormat);
              }
          }
      

      protected void BindDataList()
      {
      DirectoryInfo dir = new DirectoryInfo(MapPath("~/Images"));
      FileInfo[] files = dir.GetFiles();
      ArrayList listItems = new ArrayList();
      foreach (FileInfo info in files)
      {
      listItems.Add(info);
      }
      dtlist.DataSource = listItems;
      dtlist.DataBind();
      }

      S Offline
      S Offline
      Software2007
      wrote on last edited by
      #2

      Anyone?

      1 Reply Last reply
      0
      • S Software2007

        -I am trying to learn how to load up a Gallery of images to asp website.I understand I could load thumbnail images to reduce original size. Here is some code I tried after googling, but its still loading original size. -Also, Can I make these images look more alive, like clikable, or double their size whenclicked?

        protected void btnsave_Click(object sender, EventArgs e)
        {
        string filename = Path.GetFileName(fileupload1.PostedFile.FileName);
        string targetPath = Server.MapPath("Images/" + filename);
        Stream strm = fileupload1.PostedFile.InputStream;
        var targetFile = targetPath;
        //Based on scalefactor image size will vary
        GenerateThumbnails(0.5, strm, targetFile);
        BindDataList();
        }

        private void GenerateThumbnails(double scaleFactor, Stream sourcePath,string targetPath)
        {
        using (var image = System.Drawing.Image.FromStream(sourcePath))
        {
        // can given width of image as we want
        var newWidth = (int)(image.Width * scaleFactor);
        // can given height of image as we want
        var newHeight = (int)(image.Height * scaleFactor);
        var thumbnailImg = new Bitmap(newWidth, newHeight);
        var thumbGraph = Graphics.FromImage(thumbnailImg);

                    thumbGraph.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                    thumbGraph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    thumbGraph.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
                    thumbGraph.DrawImage(image, imageRectangle);
                    thumbnailImg.Save(targetPath, image.RawFormat);
                }
            }
        

        protected void BindDataList()
        {
        DirectoryInfo dir = new DirectoryInfo(MapPath("~/Images"));
        FileInfo[] files = dir.GetFiles();
        ArrayList listItems = new ArrayList();
        foreach (FileInfo info in files)
        {
        listItems.Add(info);
        }
        dtlist.DataSource = listItems;
        dtlist.DataBind();
        }

        S Offline
        S Offline
        Sean A Hanley
        wrote on last edited by
        #3

        For generating thumbnails, take a look at the GetThumbnailImage method of the Image class. It's perfect for this — I've used it for just this purpose in the past (displaying small images to save bandwidth and load time on an ASP.NET site). I can't help you with displaying them though, as you don't show any markup or your Page_Load method and the like. Remember, though, that if you are generating thumbnails on the fly in memory on the server, these do not exist anywhere on disk. When the HTML is loaded in the client browser, the img element needs an src URL. This is, as you probably know, typically a direct link to a .jpg somewhere on your webserver virtual directory or something. Usually it is a disk file. Since these are not really saved, you can't link to a file. But you have to link to something! What we did is create an aspx page that returns image data (not html). You can find a good guide to this here (which also shows using the GetThumbnailImage method I mentioned before). You then will basically use an Image control and set its ImageUrl to MakeThumbnail.aspx?file=picture.jpg or the like. In our case, we had images stored in a database, so we used an integer id instead of a file name.

        1 Reply Last reply
        0
        • S Software2007

          -I am trying to learn how to load up a Gallery of images to asp website.I understand I could load thumbnail images to reduce original size. Here is some code I tried after googling, but its still loading original size. -Also, Can I make these images look more alive, like clikable, or double their size whenclicked?

          protected void btnsave_Click(object sender, EventArgs e)
          {
          string filename = Path.GetFileName(fileupload1.PostedFile.FileName);
          string targetPath = Server.MapPath("Images/" + filename);
          Stream strm = fileupload1.PostedFile.InputStream;
          var targetFile = targetPath;
          //Based on scalefactor image size will vary
          GenerateThumbnails(0.5, strm, targetFile);
          BindDataList();
          }

          private void GenerateThumbnails(double scaleFactor, Stream sourcePath,string targetPath)
          {
          using (var image = System.Drawing.Image.FromStream(sourcePath))
          {
          // can given width of image as we want
          var newWidth = (int)(image.Width * scaleFactor);
          // can given height of image as we want
          var newHeight = (int)(image.Height * scaleFactor);
          var thumbnailImg = new Bitmap(newWidth, newHeight);
          var thumbGraph = Graphics.FromImage(thumbnailImg);

                      thumbGraph.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                      thumbGraph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                      thumbGraph.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                      var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
                      thumbGraph.DrawImage(image, imageRectangle);
                      thumbnailImg.Save(targetPath, image.RawFormat);
                  }
              }
          

          protected void BindDataList()
          {
          DirectoryInfo dir = new DirectoryInfo(MapPath("~/Images"));
          FileInfo[] files = dir.GetFiles();
          ArrayList listItems = new ArrayList();
          foreach (FileInfo info in files)
          {
          listItems.Add(info);
          }
          dtlist.DataSource = listItems;
          dtlist.DataBind();
          }

          B Offline
          B Offline
          BillWoodruff
          wrote on last edited by
          #4

          Check out ... here on CP : "LeftImage - An Image Optimization Library with Fluent Interface"[^].

          "Last year I went fishing with Salvador Dali. He was using a dotted line. He caught every other fish." Steven Wright

          1 Reply Last reply
          0
          • S Software2007

            -I am trying to learn how to load up a Gallery of images to asp website.I understand I could load thumbnail images to reduce original size. Here is some code I tried after googling, but its still loading original size. -Also, Can I make these images look more alive, like clikable, or double their size whenclicked?

            protected void btnsave_Click(object sender, EventArgs e)
            {
            string filename = Path.GetFileName(fileupload1.PostedFile.FileName);
            string targetPath = Server.MapPath("Images/" + filename);
            Stream strm = fileupload1.PostedFile.InputStream;
            var targetFile = targetPath;
            //Based on scalefactor image size will vary
            GenerateThumbnails(0.5, strm, targetFile);
            BindDataList();
            }

            private void GenerateThumbnails(double scaleFactor, Stream sourcePath,string targetPath)
            {
            using (var image = System.Drawing.Image.FromStream(sourcePath))
            {
            // can given width of image as we want
            var newWidth = (int)(image.Width * scaleFactor);
            // can given height of image as we want
            var newHeight = (int)(image.Height * scaleFactor);
            var thumbnailImg = new Bitmap(newWidth, newHeight);
            var thumbGraph = Graphics.FromImage(thumbnailImg);

                        thumbGraph.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                        thumbGraph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                        thumbGraph.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                        var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
                        thumbGraph.DrawImage(image, imageRectangle);
                        thumbnailImg.Save(targetPath, image.RawFormat);
                    }
                }
            

            protected void BindDataList()
            {
            DirectoryInfo dir = new DirectoryInfo(MapPath("~/Images"));
            FileInfo[] files = dir.GetFiles();
            ArrayList listItems = new ArrayList();
            foreach (FileInfo info in files)
            {
            listItems.Add(info);
            }
            dtlist.DataSource = listItems;
            dtlist.DataBind();
            }

            B Offline
            B Offline
            BillWoodruff
            wrote on last edited by
            #5

            Another route to consider is to use the newer features of CSS Image transformations (which I have not tried myself)? A recent SitePoint article; although its focus is on background-image transformation, the article implies a broader usage: "Scaling, skewing and rotating any element is possible with the CSS3 transform property. It’s supported in all modern browsers (with vendor prefixes) and degrades gracefully."[^] good luck, Bill

            "Last year I went fishing with Salvador Dali. He was using a dotted line. He caught every other fish." Steven Wright

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups