FrameAreaImage control

最近在项目中,需要把一个中空的图片放大绘制(图片如下),而不失真。

image 
试了picture congtrol,但是失真。最终我用customize control解决了问题。

代码里面见真相。

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.ComponentModel;
   4:  using System.Drawing;
   5:  using System.Data;
   6:  using System.Linq;
   7:  using System.Text;
   8:  using System.Windows.Forms;
   9:  using System.Drawing.Drawing2D;
  10:   
  13:      public partial class FrameAreaImage : Control
  14:      {
  15:          #region [ Private Members... ]
  16:          private int ConerLength = 40;
  17:          private int FrameImageEdgeLength = 30;
  18:          #endregion
  19:          #region [ Constructors / Destructors... ]
  20:          public FrameAreaImage()
  21:          {
  22:              InitializeComponent();
  23:          }
  24:          #endregion
  25:          #region [ Accessors / Manipulators... ]
  26:          public Bitmap Image
  27:          {
  28:              get;
  29:              set;
  30:          }
  31:   
  32:          public int FrameShadowSize
  33:          {
  34:              get;
  35:              set;
  36:          }
  37:          #endregion
  38:          #region [ Overrides... ]
  39:   
  40:          protected override void OnPaint(PaintEventArgs e)
  41:          {
  42:              Graphics g = e.Graphics;
  43:   
  44:              if (Image != null)
  45:              {
  46:                  //////////////////////////////////////////////////////////////////////////
  47:                  // Draw the Corner
  48:                  //////////////////////////////////////////////////////////////////////////
  49:                  var image = Image.Clone(new Rectangle(0, 0, ConerLength, ConerLength),
  50:                                          System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  51:                  // TopLeft 
  52:                  g.DrawImage(image, 0, 0, ConerLength, ConerLength);
  53:                  image.Dispose();
  54:   
  55:   
  56:                  // Left Bottom
  57:                  image = Image.Clone(new Rectangle(0, Image.Height - ConerLength, ConerLength, ConerLength),
  58:                                      System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  59:                  g.DrawImage(image, new Point(1, this.Height - ConerLength));
  60:                  image.Dispose();
  61:   
  62:                  // Right Bottom
  63:                  image = Image.Clone(new Rectangle(Image.Width - ConerLength, Image.Height - ConerLength, ConerLength, ConerLength),
  64:                                      System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  65:                  g.DrawImage(image, new Point(this.Width - ConerLength, this.Height - ConerLength));
  66:                  image.Dispose();
  67:   
  68:                  // Top Right
  69:                  image = Image.Clone(new Rectangle(Image.Width - ConerLength, 0, ConerLength, ConerLength),
  70:                                          System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  71:                  g.DrawImage(image, new Point(this.Width - ConerLength, 0));
  72:                  image.Dispose();
  73:   
  74:                  //////////////////////////////////////////////////////////////////////////
  75:                  // Draw the Edge Shadow
  76:                  //////////////////////////////////////////////////////////////////////////
  77:           
  78:                  // Left
  79:                  image = Image.Clone(new Rectangle(0, ConerLength, FrameShadowSize, FrameImageEdgeLength),
  80:                                          System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  81:                  TextureBrush tBrush = new TextureBrush(image);
  82:                  tBrush.WrapMode = WrapMode.TileFlipY;
  83:                  g.FillRectangle(tBrush, new Rectangle(0, ConerLength, FrameShadowSize, this.Height - 2 * ConerLength));
  84:                  image.Dispose();
  85:                  tBrush.Dispose();
  86:   
  87:                  // Bottom
  88:                  g.DrawImage(Image.Clone(new Rectangle(FrameImageEdgeLength, Image.Height - FrameShadowSize, FrameImageEdgeLength, FrameShadowSize),
  89:                                          System.Drawing.Imaging.PixelFormat.Format32bppArgb),
  90:                                          ConerLength - 2, this.Height - FrameShadowSize - 1, this.Width - 2 * ConerLength + ConerLength / 2, FrameShadowSize);
  91:   
  92:                  
  93:                     g.DrawImage(Image.Clone(new Rectangle(Image.Width - FrameShadowSize, ConerLength, FrameShadowSize, FrameImageEdgeLength),
  94:                                          System.Drawing.Imaging.PixelFormat.Format32bppArgb),
  95:                                          this.Width - FrameShadowSize - 1, ConerLength - 2, FrameShadowSize, this.Height - 2 * ConerLength + ConerLength / 2);
  96:   
  97:                  // Top
  98:                  g.DrawImage(Image.Clone(new Rectangle(FrameImageEdgeLength, 0, FrameImageEdgeLength, FrameShadowSize),
  99:                              System.Drawing.Imaging.PixelFormat.Format32bppArgb),
 100:                              ConerLength, 0, this.Width - 2 * ConerLength + FrameShadowSize, FrameShadowSize);
 101:              }
 102:   
 103:              base.OnPaint(e);
 104:          }
 105:          #endregion
 106:      }

转载于:https://www.cnblogs.com/jalenwang/archive/2011/10/22/2221273.html

猜你喜欢

转载自blog.csdn.net/weixin_33862993/article/details/93492862