c# - 如何缩小(缩放)整个图形结构?

标签 c# winforms graphics scaling graphics2d

我试图将很多矩形放入一个位图中,该位图将显示在一个图片框中。在我的实际代码中,我计算出可以包含所有这些矩形的总宽度和高度,然后我将其除以位图的大小,以获得我的比例因子。问题是我不知道如何执行缩放。下面的代码是我需要做的一个简单版本。

请记住,我不能依赖图片框的缩放能力(拉伸(stretch)),而且我不想简单地将缩放应用于所有矩形的宽度和高度,因为在我的真实代码中它不会工作得很好。我需要一种在图形中缩小它的方法。重要的是位图保持与原来相同的大小 (300 X 300)。谢谢。下面的代码是我到目前为止得到的,但大小没有任何变化。

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication22
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Bitmap BM = new System.Drawing.Bitmap(300, 300);
        Pen PenTest = new System.Drawing.Pen(Brushes.Red);
        private void Form1_Load(object sender, EventArgs e)
        {
             using (Graphics GR = Graphics.FromImage(BM))
            {

                GR.DrawRectangle(PenTest, new Rectangle(0,0,500,500));

                 // I need a scale of 0.60 in this example, because 300/500 = .6

                GR.ScaleTransform(.6F, .6F);//Doesn't Work. No Change at all in the size of the rectangle.


            }


            pictureBox1.Image = BM;
        }

    }
}

最佳答案

Graphics.ScaleTransform执行转换但不绘制任何内容。

在对图形对象执行转换后,您需要绘制一个矩形:

 using (Graphics GR = Graphics.FromImage(BM))
 {
     // ....

     GR.ScaleTransform(.6F, .6F);
     GR.DrawRectangle(PenTest, new Rectangle(0,0,500,500));


 }

关于c# - 如何缩小(缩放)整个图形结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13389467/

相关文章:

c# - 需要删除我自己的应用程序正在使用的文件

math - 在给定旋转矩阵的情况下找到顶点的新位置

r - 如何在ggplot2中使用希腊符号?

c# - 从 Invoke 中关闭表单

c# - Windows 8 - 花哨的进度条 API?

graphics - 如何更新 gfx-rs 中的索引缓冲区?

c# - Active Directory 中的搜索过滤器无效

C# PrintDocument 和打印机状态

c# - 如何将 ASP.NET MVC 中的 HTML <select> 多个属性的更新值插入到数据库?

c# - 如何导航到 Blazor 应用程序中的 ASP.NET Core MVC Controller ?