Guest User

Untitled

a guest
Nov 21st, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.74 KB | None | 0 0
  1. var request = WebRequest.Create("http://www.gravatar.com/avatar/6810d91caff032b202c50701dd3af745?d=identicon&r=PG");
  2.  
  3. using (var response = request.GetResponse())
  4. using (var stream = response.GetResponseStream())
  5. {
  6. pictureBox1.Image = Bitmap.FromStream(stream);
  7. }
  8.  
  9. yourPictureBox.ImageLocation = "http://www.gravatar.com/avatar/6810d91caff032b202c50701dd3af745?d=identicon&r=PG"
  10.  
  11. #region Image Utilities
  12.  
  13. /// <summary>
  14. /// Loads an image from a URL into a Bitmap object.
  15. /// Currently as written if there is an error during downloading of the image, no exception is thrown.
  16. /// </summary>
  17. /// <param name="url"></param>
  18. /// <returns></returns>
  19. public static Bitmap LoadPicture(string url)
  20. {
  21. System.Net.HttpWebRequest wreq;
  22. System.Net.HttpWebResponse wresp;
  23. Stream mystream;
  24. Bitmap bmp;
  25.  
  26. bmp = null;
  27. mystream = null;
  28. wresp = null;
  29. try
  30. {
  31. wreq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
  32. wreq.AllowWriteStreamBuffering = true;
  33.  
  34. wresp = (System.Net.HttpWebResponse)wreq.GetResponse();
  35.  
  36. if ((mystream = wresp.GetResponseStream()) != null)
  37. bmp = new Bitmap(mystream);
  38. }
  39. catch
  40. {
  41. // Do nothing...
  42. }
  43. finally
  44. {
  45. if (mystream != null)
  46. mystream.Close();
  47.  
  48. if (wresp != null)
  49. wresp.Close();
  50. }
  51.  
  52. return (bmp);
  53. }
  54.  
  55. /// <summary>
  56. /// Takes in an image, scales it maintaining the proper aspect ratio of the image such it fits in the PictureBox's canvas size and loads the image into picture box.
  57. /// Has an optional param to center the image in the picture box if it's smaller then canvas size.
  58. /// </summary>
  59. /// <param name="image">The Image you want to load, see LoadPicture</param>
  60. /// <param name="canvas">The canvas you want the picture to load into</param>
  61. /// <param name="centerImage"></param>
  62. /// <returns></returns>
  63.  
  64. public static Image ResizeImage(Image image, PictureBox canvas, bool centerImage )
  65. {
  66. if (image == null || canvas == null)
  67. {
  68. return null;
  69. }
  70.  
  71. int canvasWidth = canvas.Size.Width;
  72. int canvasHeight = canvas.Size.Height;
  73. int originalWidth = image.Size.Width;
  74. int originalHeight = image.Size.Height;
  75.  
  76. System.Drawing.Image thumbnail =
  77. new Bitmap(canvasWidth, canvasHeight); // changed parm names
  78. System.Drawing.Graphics graphic =
  79. System.Drawing.Graphics.FromImage(thumbnail);
  80.  
  81. graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
  82. graphic.SmoothingMode = SmoothingMode.HighQuality;
  83. graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
  84. graphic.CompositingQuality = CompositingQuality.HighQuality;
  85.  
  86. /* ------------------ new code --------------- */
  87.  
  88. // Figure out the ratio
  89. double ratioX = (double)canvasWidth / (double)originalWidth;
  90. double ratioY = (double)canvasHeight / (double)originalHeight;
  91. double ratio = ratioX < ratioY ? ratioX : ratioY; // use whichever multiplier is smaller
  92.  
  93. // now we can get the new height and width
  94. int newHeight = Convert.ToInt32(originalHeight * ratio);
  95. int newWidth = Convert.ToInt32(originalWidth * ratio);
  96.  
  97. // Now calculate the X,Y position of the upper-left corner
  98. // (one of these will always be zero)
  99. int posX = Convert.ToInt32((canvasWidth - (image.Width * ratio)) / 2);
  100. int posY = Convert.ToInt32((canvasHeight - (image.Height * ratio)) / 2);
  101.  
  102. if (!centerImage)
  103. {
  104. posX = 0;
  105. posY = 0;
  106. }
  107. graphic.Clear(Color.White); // white padding
  108. graphic.DrawImage(image, posX, posY, newWidth, newHeight);
  109.  
  110. /* ------------- end new code ---------------- */
  111.  
  112. System.Drawing.Imaging.ImageCodecInfo[] info =
  113. ImageCodecInfo.GetImageEncoders();
  114. EncoderParameters encoderParameters;
  115. encoderParameters = new EncoderParameters(1);
  116. encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality,
  117. 100L);
  118.  
  119. Stream s = new System.IO.MemoryStream();
  120. thumbnail.Save(s, info[1],
  121. encoderParameters);
  122.  
  123. return Image.FromStream(s);
  124. }
  125.  
  126. #endregion
  127.  
  128. using System.Windows.Forms;
  129. using System.Drawing.Drawing2D;
  130. using System.IO;
  131. using System.Drawing.Imaging;
  132. using System.Text.RegularExpressions;
  133. using System.Drawing;
  134.  
  135. ImageUtil.ResizeImage(ImageUtil.LoadPicture( "http://someurl/img.jpg", pictureBox1, true);
  136.  
  137. pictureBox1.LoadAsync(@"http://google.com/test.png");
Add Comment
Please, Sign In to add comment