在我的應用程序中,我接受來自用戶的圖像。如果圖像大於指定大小,那麼我會縮小到適當的大小並保存在數據庫中。我正在使用FJCore庫來縮放圖像。該庫適用於JPEG圖像。但它不支持PNG圖像。看來圖書館最近沒有更新。任何想法如何在Silverlight中完成?在Silverlight中縮放PNG圖像
0
A
回答
0
您可以做的是創建一個新的Image元素,並將其源設置爲從該流創建的可寫位圖,但不要將此Image元素添加到可視樹中。創建另一個需要的最終大小的WriteableBitmap,然後在此WriteableBitmap上調用render來傳遞Image元素和ScaleTransform以將圖像大小調整爲適當的大小。然後,您可以使用第二個WriteableBitmap作爲第二個Image元素的源,並將其添加到可視化樹中。
0
我用WriteableBitmapEx項目做了這個。這是代碼,如果有人需要它。
private void ShowCustomImageButton_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openDialog = new OpenFileDialog();
openDialog.Multiselect = false;
openDialog.Filter = "PNG Files|*.PNG";
bool? userClickedOK = openDialog.ShowDialog();
if (userClickedOK == true)
{
BitmapImage image = new BitmapImage();
// get image that user has selected.
image.SetSource(openDialog.File.OpenRead());
WriteableBitmap wrtbmp = new WriteableBitmap(image);
// resize image if needed.
wrtbmp = wrtbmp.Resize(64, 64, WriteableBitmapExtensions.Interpolation.NearestNeighbor);
var img = wrtbmp.ToImage();
// convert image into file stream.
Stream filestram = img.ToStream();
filestram.Position = 0;
using (filestram)
{
// convert file stream into memory stream.
var memoryStream = new MemoryStream();
byte[] aryBuffer = new byte[16384];
int nRead = filestram.Read(aryBuffer, 0, aryBuffer.Length);
while (nRead > 0)
{
memoryStream.Write(aryBuffer, 0, nRead);
nRead = filestram.Read(aryBuffer, 0, aryBuffer.Length);
}
// use following line to convert in bytes and save into database.
memoryStream.ToArray();
imgCustomImage.Source = CreateBitmapImage(memoryStream);
}
}
}
private BitmapImage CreateBitmapImage(MemoryStream memoryStream)
{
if ((memoryStream == null) || (memoryStream.Length == 0))
return null;
var image = new BitmapImage();
image.SetSource(memoryStream);
return image;
}
相關問題
- 1. 在Silverlight中放大平移和縮放圖像
- 2. 在Unity5中縮放PNG? - Bountie
- 3. 在光環中的縮放圖像中縮放圖像
- 4. 在php中動態縮放圖像jpg/png/gif
- 5. 在ruby中縮放圖像
- 6. 在as3中縮放圖像?
- 7. 在CSS中縮放圖像
- 8. 在GTK中縮放圖像
- 9. 在HTML中縮放圖像
- 10. 在GWT中縮放圖像
- 11. 在ScrollView中縮放圖像
- 12. 在gridview中縮放圖像
- 13. 縮放圖像
- 14. 圖像縮放
- 15. 使用Silverlight進行圖像縮放(縮小尺寸)
- 16. 圖像縮放/成長在縮略圖
- 17. 如何在iPhone中縮放圖像而不縮放子視圖?
- 18. 爲圖像縮放圖像
- 19. 縮放放置在圖像視圖中的圖像的控件?
- 20. 在滾動視圖中縮放圖像
- 21. Silverlight:爲什麼png圖像不顯示?
- 22. 縮放和滾動的圖像在Silverlight 4
- 23. 中心圖像縮放滾動圖像
- 24. android中的縮放圖像
- 25. 縮放PrimeFaces中的圖像
- 26. 縮放iTextSharp中的圖像
- 27. 在Android上縮放圖像
- 28. Silverlight控件視圖圖像放大/縮小?
- 29. 在png圖像上拖放顏色
- 30. 縮放圖像CSS
我還需要將此圖像轉換爲字節並在下次使用這些字節時重新呈現它。這種技術在這種情況下會起作用嗎? –