簡體   English   中英

WPF應用程序中的圖像控件無法正確顯示圖像

[英]Image control in wpf application doesn't show image properly

如果實際圖像的大小大於控件的屬性,我會扭曲圖像。 例如:圖像為800 x 600,控制尺寸僅為200 x 200。

System.Windows.Controls.Image im = new System.Windows.Controls.Image();
im.Stretch = Stretch.None ;
im.Width = 200;
im.Height = 200;
im.Margin = new Thickness(5, 5, 5, 5);
im.Source = loadImageFromArray(m_imgs[f]);

private BitmapSource loadImageFromArray(Byte[] imageData)
{
    using (MemoryStream ms = new MemoryStream(imageData))
    {
        var decoder = BitmapDecoder.Create(ms,
            BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnDemand);
        return decoder.Frames[0];
    }
}

試圖與Image.Stretch一起玩,但是沒有一個值不能正確顯示。 我希望控件在不裁剪的情況下調整圖像大小並顯示它。 圖片為jpeg格式。

VS2013,WPF,Netfw 4.0。

UPD:抱歉,我找到了答案。 問題包含在BitmapCacheOptions中。 使用BitmapCacheOption.OnLoad,一切都很好。 不知道為什么OnDemand僅適用於小圖像。 將帖子作為答案。

問題出在緩存選項中。 正如Clemens所指出的,如果您希望在創建位圖后關閉bitmapStream ,則需要選擇OnLoad緩存選項。

設置“ Uniform ”時,將調整內容的大小以適合目標尺寸,同時保留其原始縱橫比。

   System.Windows.Controls.Image im = new System.Windows.Controls.Image();
   im.Stretch = Stretch.Uniform;
   im.MaxHeight = 200;
   im.MaxWidth = 200;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM