![](/img/trans.png)
[英]c# resize image and fit it into the square while preserving aspect ratio
[英]Fit image to ShaderResourceView and preserve aspect ratio
我正在將圖像加載到ShaderResourceView中以用作背景圖像。 這是我目前正在使用的:
ShaderResourceView.FromFile(_device, imagepath);
它將圖像加載到背景中,但不考慮縱橫比而將兩個維度拉伸。 有沒有一種方法可以將圖像加載到背景中並保持寬高比?
理想情況下,ShaderResourceView將提供一種設置來自動尊重圖像外觀,但是下面是第二個最佳選擇。
SO上的現有帖子將圖像調整為指定的大小,然后用空格填充任何多余的空間並創建一個新圖像。 鏈接: https : //stackoverflow.com/a/2001462/978622
另一篇文章介紹了如何輕松地從Image轉換為bytearray,這是ShaderResourceView可以從其創建着色器的類型之一。 鏈接: https : //stackoverflow.com/a/3801289/978622
我的代碼將所有內容組合在一起:
var original = Image.FromFile(path);
Image resized = FixedSize(original, actualWidth, actualHeight);
var byteImage = ImageToByteArray(resized);
var srv = ShaderResourceView.FromMemory(_device, byteImage);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.