C#如何从“System.Drawing.RectangleF”转换为“System.Drawing.Rectangle”
时间: 2023-12-10 19:03:25 浏览: 47
你可以通过以下方法将 `System.Drawing.RectangleF` 转换为 `System.Drawing.Rectangle`:
```csharp
// 创建一个矩形浮点类型
RectangleF rectF = new RectangleF(10.5f, 20.5f, 30.5f, 40.5f);
// 将浮点类型转换为整数类型矩形
Rectangle rect = Rectangle.Round(rectF);
```
在上面的例子中,`Rectangle.Round()` 方法将 `RectangleF` 对象转换为 `Rectangle` 对象。这个方法将四舍五入 `RectangleF` 中的坐标值,并返回一个整数类型的矩形。
相关问题
e.Graphics.FillRectangle属性
e.Graphics.FillRectangle 是 C# 中 Graphics 类的一个方法,用于在指定的位置绘制一个填充的矩形。它的语法如下:
```
public void FillRectangle (System.Drawing.Brush brush, System.Drawing.RectangleF rect);
```
其中,brush 表示填充矩形的画刷,rect 表示矩形在绘图区域中的位置和大小。比如,以下代码会在 PictureBox 控件中绘制一个红色的矩形:
```csharp
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Brush brush = new SolidBrush(Color.Red);
RectangleF rect = new RectangleF(10, 10, 100, 50);
e.Graphics.FillRectangle(brush, rect);
}
```
这段代码是在 PictureBox 控件的 Paint 事件中调用的,通过创建一个红色的画刷和一个矩形,然后调用 FillRectangle 方法进行绘制。
C#DrawImage(Image image, RectangleF destRect, RectangleF srcRect, GraphicsUnit srcUnit)功能及用法
C#中的DrawImage()方法是绘制图像的方法之一,它可以将指定的图像绘制到 Graphics 对象的指定位置,并且可以指定绘制的源矩形和目标矩形。
方法参数说明:
- Image image: 要绘制的图像。
- RectangleF destRect: 目标矩形,指定了图像在 Graphics 对象中的位置和大小。
- RectangleF srcRect: 源矩形,指定了要绘制的图像的位置和大小。
- GraphicsUnit srcUnit: 指定源矩形的单位。
方法用法示例:
```csharp
// 创建一个 Graphics 对象
Graphics g = this.CreateGraphics();
// 创建一个 Bitmap 对象,作为要绘制的图像
Bitmap image = new Bitmap("example.png");
// 定义目标矩形,指定了图像在 Graphics 对象中的位置和大小
RectangleF destRect = new RectangleF(100, 100, 200, 200);
// 定义源矩形,指定了要绘制的图像的位置和大小
RectangleF srcRect = new RectangleF(0, 0, image.Width, image.Height);
// 绘制图像
g.DrawImage(image, destRect, srcRect, GraphicsUnit.Pixel);
// 释放资源
g.Dispose();
image.Dispose();
```
上面的示例中,我们首先创建了一个 Graphics 对象和一个 Bitmap 对象,然后定义了目标矩形和源矩形,最后调用 DrawImage() 方法将图像绘制到 Graphics 对象中。最后别忘了释放资源。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)