图像或位图的 C# 麦田圈
问题说明
我相信这很容易,但似乎找不到任何有答案的人.我有一个图像,我需要从该图像中剪出一个圆形甚至其他形状.我需要将此代码放在 .net c# 中.我在课堂上这样做,所以它不是 wpf 或 winform.我需要传递 x 和 y pos 以及圆的大小.
I am sure its easy but can't seem to find anyone that has the answer. I have an image and I need to cut a circle or even other shapes out of that image. I need this code to be in .net c#. I am doing this in a class so its not wpf or a winform. I will need to pass x and y pos and the size of the circle.
其他选项是 AForge、ImageStatistics.我需要得到一个圆圈(图像的一部分)并得到 StdDev.
Other option is AForge, ImageStatistics. I need to get a circle (part of the image) and get StdDev.
感谢您的帮助.安德鲁
-- 更新到克里斯的帖子.
-- update to chris post.
这是克里斯在 c# 中的帖子.不像他的那么干净,但这是一个开始.
Here is chris post in c#. Not as clean as his but its a start.
public System.Drawing.Image x(string sourceFile, int circleUpperLeftX, int circleUpperLeftY, int circleDiameter)
{
Bitmap SourceImage = new Bitmap(System.Drawing.Image.FromFile(sourceFile));
Rectangle CropRect = new Rectangle(circleUpperLeftX, circleUpperLeftY, circleDiameter, circleDiameter);
Bitmap CroppedImage = SourceImage.Clone(CropRect, SourceImage.PixelFormat);
TextureBrush TB = new TextureBrush(CroppedImage);
Bitmap FinalImage = new Bitmap(circleDiameter, circleDiameter);
Graphics G = Graphics.FromImage(FinalImage);
G.FillEllipse(TB, 0, 0, circleDiameter, circleDiameter);
return FinalImage;
}
正确答案
您可以使用 纹理刷.下面的代码将图像裁剪为正方形,然后将其加载到纹理画笔中,最后使用该画笔绘制椭圆/圆:
You can use a TextureBrush. The code below crops the image to a square, then loads that into a texture brush and finally draws an ellipse/circle using that brush:
Private Shared Function CropImageToCircle(ByVal sourceFile As String, ByVal circleUpperLeftX As Integer, ByVal circleUpperLeftY As Integer, ByVal circleDiameter As Integer) As Image
''//Load our source image
Using SourceImage As New Bitmap(Image.FromFile(sourceFile))
''//Create a rectangle that crops a square of our image
Dim CropRect As New Rectangle(circleUpperLeftX, circleUpperLeftY, circleDiameter, circleDiameter)
''//Crop the image to that square
Using CroppedImage = SourceImage.Clone(CropRect, SourceImage.PixelFormat)
''//Create a texturebrush to draw our circle with
Using TB As New TextureBrush(CroppedImage)
''//Create our output image
Dim FinalImage As New Bitmap(circleDiameter, circleDiameter)
''//Create a graphics object to draw with
Using G = Graphics.FromImage(FinalImage)
''//Draw our cropped image onto the output image as an ellipse with the same width/height (circle)
G.FillEllipse(TB, 0, 0, circleDiameter, circleDiameter)
Return FinalImage
End Using
End Using
End Using
End Using
End Function
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tangicbff
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
检查邮件是否发送成功
it1352 07-25 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20