Clip an image in a specific shape .NET

后端 未结 2 1308
时光取名叫无心
时光取名叫无心 2021-01-26 01:25

I have a page in my MVC4 project where user can add its company logo using the file upload control. These images/logos are then shown on map in mobile application. We need to cr

2条回答
  •  隐瞒了意图╮
    2021-01-26 02:09

      // Following code derives a cutout bitmap using a
      // scizzor path as a clipping region (like Paint would do)
      // Result bitmap has a minimal suitable size, pixels outside 
      // the clipping path will be white.
    
      public static Bitmap ApplyScizzors(Bitmap bmpSource, List pScizzor)
        {
            GraphicsPath graphicsPath = new GraphicsPath();   // specified Graphicspath          
            graphicsPath.AddPolygon(pScizzor.ToArray());      // add the Polygon
            var rectCutout = graphicsPath.GetBounds();        // find rectangular range           
            Matrix m = new Matrix();
            m.Translate(-rectCutout.Left, -rectCutout.Top);   // translate clip to (0,0)
            graphicsPath.Transform(m);
            Bitmap bmpCutout = new Bitmap((int)(rectCutout.Width), (int)(rectCutout.Height));  // target
            Graphics graphicsCutout = Graphics.FromImage(bmpCutout);
            graphicsCutout.Clip = new Region(graphicsPath);
            graphicsCutout.DrawImage(bmpSource, (int)(-rectCutout.Left), (int)(-rectCutout.Top)); // draw
            graphicsPath.Dispose();
            graphicsCutout.Dispose();
            return bmpCutout;
        }
    

提交回复
热议问题