亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將位圖轉換為 ImageSource 后資源 png 質量下降

將位圖轉換為 ImageSource 后資源 png 質量下降

C#
慕容708150 2023-04-29 09:49:50
我有一個透明的 PNG,當我將它轉換為 ImageSource 時,它會損失很多質量。我所做的轉換如下:public static ImageSource ToImageSource()    {   Bitmap bitmap = Properties.Resources.Image;        IntPtr hBitmap = bitmap.GetHbitmap();        ImageSource wpfBitmap = Imaging.CreateBitmapSourceFromHBitmap(            hBitmap,            IntPtr.Zero,            Int32Rect.Empty,            BitmapSizeOptions.FromEmptyOptions());        RenderOptions.SetBitmapScalingMode(wpfBitmap, BitmapScalingMode.HighQuality);        return wpfBitmap;    }但是質量真的很差。當我直接調用計算機上的文件時,質量是正確的:<DataGridTemplateColumn Width="14" IsReadOnly="True">                <DataGridTemplateColumn.CellTemplate>                    <DataTemplate>                        <Image Source="C:\Users\MyUser\Desktop\Image.png" Width="14" Height="14"></Image>                    </DataTemplate>                </DataGridTemplateColumn.CellTemplate>            </DataGridTemplateColumn>有沒有另一種方法可以在不損失質量和透明度的情況下轉換資源?
查看完整描述

3 回答

?
紅顏莎娜

TA貢獻1842條經驗 獲得超13個贊

或者...試試這個 :)


public static ImageSource ToImageSource()

{

     Bitmap bitmap = Properties.Resources.Image;

     IntPtr hBitmap = bitmap.GetHbitmap();


     ImageSource wpfBitmap = Imaging.CreateBitmapSourceFromHBitmap(

                        hBitmap,

                        IntPtr.Zero,

                        new Int32Rect(0, 0, bitmap.Width, bitmap.Height),

                        BitmapSizeOptions.FromEmptyOptions());


      DeleteObject(hBitmap);

      return wpfBitmap;

}


[System.Runtime.InteropServices.DllImport("gdi32.dll")]

public static extern bool DeleteObject(IntPtr hObject);

調用DeleteObject將釋放 GDI 句柄。


查看完整回答
反對 回復 2023-04-29
?
慕妹3146593

TA貢獻1820條經驗 獲得超9個贊

遺憾的是我無法測試您的代碼,但我認為您必須將模式設置為 NearestNeighbor


嘗試這個


public static ImageSource ToImageSource()

{   

   Bitmap bitmap = Properties.Resources.Image;

   IntPtr hBitmap = bitmap.GetHbitmap();


   ImageSource wpfBitmap = Imaging.CreateBitmapSourceFromHBitmap(

       hBitmap,

       IntPtr.Zero,

       Int32Rect.Empty,

       BitmapSizeOptions.FromEmptyOptions());

   RenderOptions.SetBitmapScalingMode(wpfBitmap, BitmapScalingMode.NearestNeighbor);


   return wpfBitmap;

}


查看完整回答
反對 回復 2023-04-29
?
回首憶惘然

TA貢獻1847條經驗 獲得超11個贊

嘗試這個:

BitmapImage image = new BitmapImage(new Uri("your image path here", UriKind.Relative));

編輯:假設您有圖像路徑。


查看完整回答
反對 回復 2023-04-29
  • 3 回答
  • 0 關注
  • 193 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號