2015-06-24 39 views
0

我想在UltraGridCell的另一個縮放模式下顯示圖片。模式應該將圖像縮放到單元格高度,並從右側剪切圖像的其餘部分以適應單元格。這很容易,如果我可以自己繪製它,因爲EmbeddableImageRenderer不允許我設置其縮放行爲(我不是在談論MaintainAspectRatio,因爲我仍然想保持縱橫比)。我試過了tutorial to embed any control in a cell。並且它與TrackBar的給定示例一起工作良好(並且在我的小型測試項目中還使用ProgressBar作爲RendererControl)。但它似乎不適用於顯示圖像的列。使用UltraControlContainerEditor嵌入任何控件不適用於圖像列

作爲一個數據源,我有一個我自己的類的列表,並顯示在網格中的圖像屬性。作爲Editor-/RendererControl,我設置了兩個常規PictureBoxes。

任何建議來解決縮放圖像的主要問題或設置任何控制圖片列(然後將處理縮放)?

回答

0

我看不到UltraControlContainerEditor不能與圖像列一起工作的任何原因,前提是您使用的控件具有的屬性需要使用Image,並且您在編輯器上指定了正確的PropertyName。但無論如何,這可能不是最有效的方法。

更好的方法是使用DrawFilter自己繪製圖像。

public class ImageScalingDrawFilter : IUIElementDrawFilter 
{ 
    bool IUIElementDrawFilter.DrawElement(DrawPhase drawPhase, ref UIElementDrawParams drawParams) 
    { 
     switch (drawPhase) 
     { 
      case DrawPhase.BeforeDrawImage: 

       ImageUIElement imageElement = (ImageUIElement)drawParams.Element; 
       Image image = imageElement.Image;      

       int availableHeight = drawParams.Element.RectInsideBorders.Height; 
       float ratio = (float)availableHeight/(float)image.Height; 

       float newHeight = image.Height * ratio; 
       float newWidth = image.Width * ratio; 

       Rectangle rect = new Rectangle(
        imageElement.Rect.X, 
        imageElement.Rect.Y, 
        (int)(newWidth), 
        (int)(newHeight) 

        ); 

       // Draw the scaled image. 
       drawParams.Graphics.DrawImage(image, rect); 

       // This tells the grid not to draw the image (since we've already drawn it). 
       return true; 
     } 

     return false; 
    } 

    DrawPhase IUIElementDrawFilter.GetPhasesToFilter(ref UIElementDrawParams drawParams) 
    { 
     UIElement element = drawParams.Element; 
     // Look for an ImageUIElement 
     if (element is ImageUIElement) 
     { 
      // Presumably, we only want to this images in cells 
      // and not every image in the entire grid, so make sure it's in a cell. 
      CellUIElement cellElement = element.GetAncestor(typeof(CellUIElement)) as CellUIElement; 
      if (null != cellElement) 
      { 
       // We could also limit this to a particular column or columns. 
       switch (cellElement.Cell.Column.Key) 
       { 
        case "Image": 
         return DrawPhase.BeforeDrawImage; 
       } 
      } 
     } 

     return DrawPhase.None; 
    } 
} 

您的DrawFilter分配到網格,像這樣:

private void Form1_Load(object sender, EventArgs e) 
    { 
     this.ultraGrid1.DrawFilter = new ImageScalingDrawFilter(); 
    } 
+0

雖然你的答案是相當晚了問題的時機我還是感謝它,我會盡快嘗試,因爲我可以,因爲你的代碼看起來很有希望我想我會在2個月內再次觸摸這些代碼。我會再次發表評論,讓任何人知道這是否是一個好的選擇。 – JoshuaBehrens

相關問題