Windows窗体C#:调整标签大小时在运行时调整标签图像大小

时间:2015-03-31 11:39:37

标签: c# winforms visual-studio resize label

这是我的问题:

我以编程方式向表单添加标签,包括一些属性,可以通过鼠标右键单击事件单击并拖动它们来在运行时调整它们的大小。

我的情况是,我通过OpenDialog添加一个以编程方式包含来自给定文件的图像的标签,我想在拉伸标签时调整此图像的大小以填充标签大小。不幸的是,我无法通过访问标签中的image.Size属性来设置运行时的大小,因为它只读...任何想法?

这是受影响的代码:

Point _oldPosition;
public static Label _ctrlActiveControl;

if (e.Button == MouseButtons.Right)
{
    _ctrlActiveControl.Cursor = Cursors.SizeNWSE;

    //Code to resize the control based on the mouse position
    Point newPosition = new Point(e.X, e.Y);
   _ctrlActiveControl.Width += (newPosition.X - _oldPosition.X);
   _ctrlActiveControl.Height += (newPosition.Y - _oldPosition.Y);

    //Some security to make sure I don't shrink the control too much
    if (_ctrlActiveControl.Width < 10) _ctrlActiveControl.Width = 10;
    if (_ctrlActiveControl.Height < 10) _ctrlActiveControl.Height = 10;

    //Here I check if the label contains an image and, if so, I should resize
    //The image to "Autofill" the label
    if (_ctrlActiveControl.Image != null)
    {
      Image image = _ctrlActiveControl.Image;
      image.Size = new Size(_ctrlActiveControl.Width, _ctrlActiveControl.Height);
    }

    _oldPosition = newPosition;
}

我想知道是否有任何方法可以做到这一点,或者我应该使用其他控制类型(我知道我可以使用其他人,但我想知道是否在添加更多变量之前,有任何可用的解决方法)。

2 个答案:

答案 0 :(得分:4)

您可以将其转换为Bitmap并使用Graphics重绘它。然后用新创建的图像替换旧图像。我不知道这是否可行,但我想这可能值得一试。

Bitmap newImage = new Bitmap(_ctrlActiveControl.Width, _ctrlActiveControl.Height);
using (Bitmap bm = new Bitmap(_ctrlActiveControl.Image))
{
    using (Graphics g = Graphics.FromImage(newImage))
    {
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        Point[] corners = { new Point(0, 0), new Point(newImage.Width, 0), new Point(0, newImage.Height) };
        g.DrawImage(bm, corners);
    }
}
_ctrlActiveControl.Image = newImage;

您需要使用System.DrawingSystem.Drawing.Drawing2D

答案 1 :(得分:2)

根据史蒂夫的建议,我已经取代了

if (_ctrlActiveControl.Image != null)
{
  Image image = _ctrlActiveControl.Image;
  image.Size = new Size(_ctrlActiveControl.Width, _ctrlActiveControl.Height);
}

并改为编写:

if (_ctrlActiveControl.Image != null)
{
     _ctrlActiveControl.Image = ResizeImage(_ctrlActiveControl.Image, _ctrlActiveControl.Width, _ctrlActiveControl.Height);
}

.....

public static Bitmap ResizeImage(Image image, int width, int height)
{
    var destRect = new Rectangle(0, 0, width, height);
    var destImage = new Bitmap(width, height);

    destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);

    using (var graphics = Graphics.FromImage(destImage))
    {
        graphics.CompositingMode = CompositingMode.SourceCopy;
        graphics.CompositingQuality = CompositingQuality.HighQuality;
        graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
        graphics.SmoothingMode = SmoothingMode.HighQuality;
        graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

        using (var wrapMode = new ImageAttributes())
        {
            wrapMode.SetWrapMode(WrapMode.TileFlipXY);
            graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
            graphics.Dispose();
        }
    }

    return destImage;
}

如果通过拉伸和收缩对标签进行过多的操作,它将变得无用(如果标签太小,我们会尝试重新放大标签,图像质量会降低)。但是,作为一个起点它可以工作。我可以根据图像源重新编码直接引用文件,所以我总是得到一个新文件...感谢您的提示。