Scale pictures (C #)

Source: Internet
Author: User

  1. Private Bitmap Zoomimage (Bitmap Bitmap, int destheight, int destwidth)
  2. {
  3. Try
  4. {
  5. System.Drawing.Image sourimage = bitmap;
  6. int width = 0, height = 0;
  7. //Proportional scaling
  8. int sourwidth = sourimage.width;
  9. int sourheight = sourimage.height;
  10. if (sourheight > Destheight | | sourwidth > Destwidth)
  11. {
  12. if ((Sourwidth * destheight) > (sourheight * destwidth))
  13. {
  14. width = destwidth;
  15. Height = (destwidth * sourheight)/sourwidth;
  16. }
  17. Else
  18. {
  19. Height = destheight;
  20. width = (sourwidth * destheight)/sourheight;
  21. }
  22. }
  23. Else
  24. {
  25. width = sourwidth;
  26. Height = sourheight;
  27. }
  28. Bitmap Destbitmap = new Bitmap (Destwidth, destheight);
  29. Graphics g = graphics.fromimage (Destbitmap);
  30. G.clear (color.transparent);
  31. //Set the drawing quality of the canvas
  32. g.compositingquality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
  33. G.smoothingmode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  34. G.interpolationmode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
  35. G.drawimage (Sourimage, new Rectangle ((destwidth-width)/2, (Destheight-height)/2, width, height), 0, 0, Sourima Ge.  Width, Sourimage.height, GraphicsUnit.Pixel);
  36. G.dispose ();
  37. //Set compression quality
  38. System.Drawing.Imaging.EncoderParameters encoderparams = new System.Drawing.Imaging.EncoderParameters ();
  39. long[] quality = new long[1];
  40. QUALITY[0] = 100;
  41. System.Drawing.Imaging.EncoderParameter Encoderparam = new System.Drawing.Imaging.EncoderParameter (  System.Drawing.Imaging.Encoder.Quality, quality);
  42. Encoderparams.param[0] = Encoderparam;
  43. Sourimage.dispose ();
  44. return destbitmap;
  45. }
  46. Catch
  47. {
  48. return bitmap;
  49. }
  50. }

Scale pictures (C #)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.