In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to achieve irregular cutting pictures in C#, the content is concise and easy to understand, it can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
C # can use GDI+ to create, modify pictures, create bitmap for pictures, and use Graphics to draw elements, points, lines, faces, etc.
Regular picture cutting, set the picture of the square or circular way, you can directly cut the picture. Cropping is saved with jpg, the cropped part of the picture will be displayed with a white background, and png can be used to set the transparency of the image.
The following introduces a way of irregular picture cutting, of course, irregular pictures can be cut, using this way, you can also cut regular pictures, the picture format is png, set the pixel transparency of png pictures.
In the development of C #, you can obtain and set the color value of each pixel on the picture. When the size of the picture is not too large, cycle to get the color of each pixel and set the transparency of each pixel, so as to achieve the trimming of the picture.
When the picture is relatively large, it is recommended to use the unsafe method to traverse the pixels of the picture. This method can be found on the Internet, and the traversal speed is several times or even dozens of times higher than that in the normal way.
To start trimming, use GraphicsPath to create a new trimmed irregular figure, usually using polygon. In this object, you need to add every point of polygon to the GraphicsPath. There is an IsVisible method in GraphicsPath to determine whether the point is in the traversal loop.
In GraphicsPath, set whether the current point is visible or not.
PointF _ pint1 = new PointF (1.0f, 1.0f)
PointF _ pint2 = new PointF (600.0f, 200.0f)
PointF _ pint3 = new PointF (200.0f, 500.0f)
PointF [] _ pointArr = new PointF [] {_ pint1, _ pint2, _ pint4, _ pint3}
GraphicsPath _ gpath = new GraphicsPath ()
_ gpath.AddPolygon (_ pointArr)
In the loop, it is found that the IsVisible method of GraphicsPath is relatively slow, which is related to a simple algorithm in GIS to determine whether the point is in the face. In fact, according to the algorithm of the map plane, the pixel in the picture is also determined whether it is in the face of the picture, so, improve the way to write a C #, refer to the Internet, the following code:
Public bool IsVisible (System.Drawing.PointF p
List points)
{
Int I, j = points.Count-1
Bool isVisible = false
For (I = 0; I)
< points.Count; i++) { if (points[i].Y < p.Y && points[j].Y >= p.Y
| | points[ j] .Y |
< p.Y && points[i].Y >= p.Y)
{
If (points [I] .X + (p.Y-points [I] .Y) / (points [j] .Y-points [I] .Y)
* (points [j] .X-points [I] .X) < p.X)
{
IsVisible =! isVisible
}
}
J = I
}
Return isVisible
}
The above content is how to achieve irregular image cutting in C#. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.