2015-03-31 61 views
3

時,這是我的問題:Windows窗體C#:在運行時調整標籤的圖像縮放標籤

我的標籤添加到窗體程序,包括一些屬性通過單擊並用鼠標右鍵拖動來調整它們的大小上運行點擊事件。

我的情況是,我通過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; 
} 

我不知道是否有任何的方式來做到這一點,或者我應該改用其他控制類型(我知道我可以使用別人,但我d想知道在添加更多變量之前是否有任何可用的解決方法)。

+1

你正在嘗試改變圖像大小,因此你可以在這裏找到一些有用的例子http://stackoverflow.com/questions/1922040/resize-an-image-c-sharp但我想你應該使用一個PictureBox的這個任務不是標籤 – Steve 2015-03-31 11:47:23

+0

PictureBox有一個SizeMode屬性,可以設置爲Stretch或Zoom,它可以自動執行您想要的操作。 – 2015-03-31 11:52:45

+0

這就是要點...如果它是一個PicBox,它會容易得多。問題是,後來在代碼中,我創建了很多標籤列表,並且我希望保持屬性的標準化,這樣我就可以處理一個對象類型而不是太多,主要是根據它們的空值。 .. – Mario 2015-03-31 11:53:21

回答

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 usings。

+0

謝謝Hjalmar。我也有問題,因爲對象是在另一個面板的子面板...長話短說,Graphics DrawImage方法也是另一個問題,因爲該對象會消失,我不想分享我的問題...謝謝你儘管提示! – Mario 2015-03-31 12:17:01

+0

哈哈。那麼整個想法就是讓一些定製的,簡單易用的報表設計器。用戶點擊這裏和那裏添加圖像,文字文本,系統數據等,以及SQL查詢中的字段,以便他們創建模板。完成模板後,我將所有帶有屬性的標籤存儲在XML文件中,以用於不同的目的。由於大多數對象都是標籤,我想(如果可能的話)總是使用這種類型的對象來保持簡單。 – Mario 2015-03-31 12:28:17

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; 
} 

如果通過拉伸和收縮打太多與標籤,就會成爲無用(圖像質量如果標籤太小,則會降低,然後我們嘗試重新放大標籤)。但是,作爲一個起點,它可以發揮作用。我可以重新編碼,直接參考文件的圖像源,所以我總是得到一個新的文件...感謝您的提示。