[英]Windows Forms C#: Resize Label image on runtime when resizing the 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;
}
我想知道是否有任何方法可以做到這一點,或者我應該使用其他控件類型( 我知道我可以使用其他類型 ,但我想知道在添加更多變量之前是否有任何可用的解決方法)。
您可以將其轉換為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.Drawing
和System.Drawing.Drawing2D
。
根據史蒂夫的建議,我已經取代了
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;
}
如果你通過拉伸和收縮來玩太多的標簽,它將變得無用(如果標簽太小,我們會嘗試重新放大標簽,圖像質量會降低)。 但是,作為一個起點它可以工作。 我可以根據圖像源重新編碼直接引用文件,所以我總是得到一個新文件...感謝您的提示。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.