Advertisement
Guest User

Untitled

a guest
Apr 17th, 2014
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. imageBox1.Image = Properties.Resources.test;
  2.  
  3. using System;
  4. using System.Drawing;
  5. using System.Windows.Forms;
  6.  
  7. class ImageBox : Panel {
  8. public ImageBox() {
  9. this.AutoScroll = true;
  10. this.DoubleBuffered = true;
  11. }
  12. private Image mImage;
  13. public Image Image {
  14. get { return mImage; }
  15. set {
  16. mImage = value;
  17. if (mImage != null) this.AutoScrollMinSize = mImage.Size;
  18. else this.AutoScrollMinSize = new Size(0, 0);
  19. this.Invalidate();
  20. }
  21. }
  22. protected override void OnPaint(PaintEventArgs e) {
  23. e.Graphics.TranslateTransform(this.AutoScrollPosition.X, this.AutoScrollPosition.Y);
  24. if (mImage != null) e.Graphics.DrawImage(mImage, 0, 0);
  25. base.OnPaint(e);
  26. }
  27. }
  28.  
  29. set {
  30. mImage = value;
  31. if (value == null) this.AutoScrollMinSize = new Size(0, 0);
  32. else {
  33. var size = value.Size;
  34. using (var gr = this.CreateGraphics()) {
  35. size.Width = (int)(size.Width * gr.DpiX / value.HorizontalResolution);
  36. size.Height = (int)(size.Height * gr.DpiY / value.VerticalResolution);
  37. }
  38. this.AutoScrollMinSize = size;
  39. }
  40. this.Invalidate();
  41. }
  42.  
  43. Graphics.DrawImage Method (Image, Int32, Int32)
  44. ...
  45. Draws the specified image, using its original physical size,
  46. at the location specified by a coordinate pair.
  47.  
  48. Remarks
  49.  
  50. An Image stores a value for pixel width and a value for horizontal resolution
  51. (dots per inch). The physical width, measured in inches, of an image is
  52. the pixel width divided by the horizontal resolution. For example,
  53. an image with a pixel width of 216 and a horizontal resolution of 72 dots
  54. per inch has a physical width of 3 inches. Similar remarks apply to pixel
  55. height and physical height.
  56.  
  57. The DrawImage method draws an image using its physical size, so the image will
  58. have its correct size in inches regardless of the resolution (dots per inch)
  59. of the display device. For example, suppose an image has a pixel width of 216
  60. and a horizontal resolution of 72 dots per inch. If you call DrawImage to
  61. draw that image on a device that has a resolution of 96 dots per inch,
  62. the pixel width of the rendered image will be (216/72)*96 = 288.
  63.  
  64. e.Graphics.DrawImage(mImage, 0, 0, mImage.Width, mImage.Height);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement