如何缩小(缩放)整个图形结构? [英] How to to shrink(scale) an entire graphics structure?

查看:181
本文介绍了如何缩小(缩放)整个图形结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将很多矩形合并到一个位图中,该位图将显示在一个图片框中。在我的真实代码中,我计算出一个矩形的总宽度和高度,可以包含所有这些矩形,然后将其除以位图的大小,以得到我的缩放因子。问题是我无法弄清楚如何执行缩放。下面的代码是我需要做的简单版本。

I’m trying to fit a lot of rectangles into a Bitmap, which will be displayed in a picturebox. In my real code, I figure out the total width and height of a rectangle that can encompass all them, and then I divide that by the size of the Bitmap, to get my scaling factor. The problem is I can’t figure out how to perform the scaling. The code below is a simple version of what I need to do.

请记住,我不能依赖于图片框的缩放功能(拉伸),而且我不想仅将缩放比例应用于所有的宽度和高度矩形,因为在我的真实代码中它不会工作得很好。我需要一种方法在Graphics中缩小它。位图保持与它相同的尺寸(300 X 300)非常重要。谢谢。下面的代码是我到目前为止所得到的,但没有什么会随着大小而改变。

Please keep in mind that I cannot rely on the picturebox’s scaling abilities (stretch), and I don’t want to simply apply the scale to the width and height of all the rectangles, because in my real code it wouldn’t work very well. I need a way to shrink it down in Graphics. It is important the Bitmap stays the same size that it is (300 X 300). Thanks. The below code is what I've gotten so far, but nothing changes with the size.

    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 执行转换,但它不会绘制任何东西。

Graphics.ScaleTransform performs the transformation but it does not draw anything.

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

You would need to then draw a rectangle after performing the transform on the graphics object:

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

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


 }

这篇关于如何缩小(缩放)整个图形结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆