简体   繁体   English

如何使用factor变量使位图适合pictureBox1?

[英]How can I use the factor variable to make the bitmap to fit in the pictureBox1?

In form1 constructor I have: 在form1构造函数中,我有:

currentfactor = factor;
bitmapwithclouds = new Bitmap(@"D:\C-Sharp\Download File\Downloading-File-Project-Version-012\Downloading File\Resources\radar090.png");
pictureBox1.Image = ResizeImage(bitmapwithclouds, new Size((int)(bitmapwithclouds.Width * factor), (int)(bitmapwithclouds.Height * factor)));

The variable factor is now 2.5 可变因子现在为2.5

The method ResizeImage is: ResizeImage方法是:

public Image ResizeImage(Image img, Size size)
{
    return new Bitmap(img, size);
}

The variable bitmapwithclouds is 512,512 and also pictureBox1 is 512,512 but once im doing the line with the factor in the constructor so the image in the pictureBox1 is resized to 1280,1280 and the pictureBox1 size is 512,512 变量bitmapwithclouds为512,512,pictureBox1也为512,512,但是一旦我与构造函数中的因数进行匹配,则pictureBox1中的图像将调整为1280,1280,pictureBox1的大小为512,512

The reason im using the factor is that later on the code i have a Load button where i load points/pixels i drawed coordinates List and draw them again on the pictureBox1 : 我使用该因子的原因是,稍后在代码上我有一个“加载”按钮,在其中加载点/像素并绘制坐标列表,然后在pictureBox1上再次绘制它们:

public static void Paint(Graphics e, double currentFactor, float kilometers)
        {
            float distance = kilometers / (float)1.09;
            Pen p;
            p = new Pen(Brushes.Green);
            if (points == null)
            {
                return;
            }
            foreach (PointF pt in points)
            {
                e.FillEllipse(Brushes.Red, (pt.X - distance) * (float)currentFactor, pt.Y * (float)currentFactor, 3f, 3f);
            }

So to see the points/pixels when i load them back on the right place im using the currentFactor variable. 因此,当我使用currentFactor变量将它们重新加载到正确的位置时,可以看到这些点/像素。

The problem is that when im running my program the image is 1280,1280 while the pictureBox1 size is 512,512 I could make some math to divide or minus to reduce the size back again: But how ? 问题是,当我运行我的程序时,图像为1280,1280,而pictureBox1的大小为512,512,我可以做一些数学运算来除以或减去以减小大小:但是如何? And is that should be the way ? 那是应该的吗?

If in the constructor i will not make 如果在构造函数中,我不会

pictureBox1.Image = ResizeImage(bitmapwithclouds, new Size((int)(bitmapwithclouds.Width * factor), (int)(bitmapwithclouds.Height * factor)));

Then the points/pixels in the List points in the painth method will be show somewhere out of the pictureBox1 area. 然后,painth方法中“列表点”中的点/像素将显示在pictureBox1区域之外的某处。

If all you really need is to resize your picturebox based on the image it contains there is a much easier way. 如果您真正需要的只是根据其中包含的图像调整图片框的大小,那么这是一种更简单的方法。

pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;

This will fit the picturebox to the content. 这将使图片框适合内容。

I made a small example demonstrating this. 我举了一个小例子来说明这一点。

Hope it's helpful to you. 希望对您有帮助。

Kind regards. 亲切的问候。

Test project 测试项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在将鼠标移到pictureBox1上时自动在pictureBox2上绘制点? - How can i make that when i move the mouse ober pictureBox1 it will draw points automatic on pictureBox2? 如何将pictureBox1中的图像替换为pictureBox1矩形区域上绘制的裁剪图像? - How can i replace the image in pictureBox1 with a crop image of a drawn on the pictureBox1 rectangle area? 如何使计时器向前或向后更改pictureBox1中显示的图像? - How can I make timer that will change the images display in pictureBox1 forward or backward? 如果用户处于鼠标按下事件中,如何检查pictureBox1鼠标离开事件? - How can i check on pictureBox1 mouse leave event if the user is in mouse down event? 我如何计算从pictureBox1的顶部到form1的顶部的距离? - How can i calculate the distance from the top of pictureBox1 and almost the top of form1? 移动trackBar1时,如何保持在图片盒1中的图像上绘制的点? - How can i keep the points im drawing on the image in the picturebox1 when moving the trackBar1? 如何解决picturebox1是null? - How to solve picturebox1 is null? 如何将生成的位图加载到PictureBox中? - How can I load a generated Bitmap into a PictureBox? 如何调整图像大小以适合图片框? - How can I resize the image to fit the picturebox? 如何将图片从PictureBox1复制到Excel? - How to copy image from PictureBox1 to excel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM