![](/img/trans.png)
[英]OutOfMemoryException when calling SetSource for BitmapImage in WP7
[英]Resizing BitmapImage wp7
如何讓它在 WP7 上工作?
private static Bitmap ResizeBitmap(Bitmap sourceBMP, int width, int height)
{
Bitmap result = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(result))
g.DrawImage(sourceBMP, 0, 0, width, height);
return result;
}
這就是我縮小從媒體庫中選擇或用相機拍攝的圖像的方法,希望對您有所幫助。 它保持縱橫比。
private void PhotoChooserTaskCompleted(object sender, PhotoResult e)
{
if (e.TaskResult != TaskResult.OK) return;
var bmp = new BitmapImage();
bmp.SetSource(e.ChosenPhoto);
var scaledDownImage = AspectScale(bmp, 640, 480);
MyImage.Source = scaledDownImage;
}
private BitmapImage AspectScale(BitmapImage img, int maxWidth, int maxHeigh)
{
double scaleRatio;
if (img.PixelWidth > img.PixelHeight)
scaleRatio = (maxWidth/(double) img.PixelWidth);
else
scaleRatio = (maxHeigh/(double) img.PixelHeight);
var scaledWidth = img.PixelWidth * scaleRatio;
var scaledHeight = img.PixelHeight * scaleRatio;
using (var mem = new MemoryStream())
{
var wb = new WriteableBitmap(img);
wb.SaveJpeg(mem, (int)scaledWidth, (int)scaledHeight, 0, 100);
mem.Seek(0, SeekOrigin.Begin);
var bn = new BitmapImage();
bn.SetSource(mem);
return bn;
}
}
這就是我如何將圖像大小減小到小於 512 kb 並將圖像保存在隔離存儲中,注意:只是 windows 手機的初學者,所以請遵守我的編碼方式
private void PhotoChooserTaskCompleted(object sender, PhotoResult e) { if (e.TaskResult.= TaskResult;OK) return;
string Imgpath = "Rafiq.jpg";
SaveToIsolatedStorage(e.ChosenPhoto, Imgpath);
}
在此方法中,我傳遞了 e.choosen 的 stream 及其文件名
並在獨立存儲中保存調整大小的圖像(圖像大小減少小於 512 kb),希望這對您有所幫助,
私人無效SaveToIsolatedStorage(流圖像流,字符串文件名){嘗試{使用(IsolatedStorageFile myIsolatedStorage=IsolatedStorageFile.GetUserStoreForApplication()){
if (myIsolatedStorage.FileExists(fileName))
{
myIsolatedStorage.DeleteFile(fileName);
}
IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(fileName);
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(imageStream);
MessageBox.Show("ImageStream :"+ imageStream.Length.ToString());
WriteableBitmap wb = new WriteableBitmap(bitmap);
// reeducing less than 524288 byte array
long compImageSize = 524288;
long originalsize = wb.ToByteArray().Length;
if ((Convert.ToInt32(originalsize)) > compImageSize)
{
WriteableBitmap wBitmap = new WriteableBitmap(bitmap);
int height = wBitmap.PixelHeight;
int width = wBitmap.PixelWidth;
while ((Convert.ToInt32(originalsize)) > compImageSize)
{
// wb.Resize(Convert.ToInt32( wb.PixelWidth / 2), Convert.ToInt32(wb.PixelHeight / 2), WriteableBitmapExtensions.Interpolation.Bilinear);
// data = ChangeDimension(bitmap, Convert.ToInt32(bitmap.PixelWidth / 2), Convert.ToInt32(bitmap.PixelHeight / 2));
using (MemoryStream stream = new MemoryStream())
{
height = Convert.ToInt32(wBitmap.PixelHeight / 2);
width = Convert.ToInt32(wBitmap.PixelWidth / 2);
wBitmap.SaveJpeg(stream, width, height, 0, 100);
stream.Seek(0, SeekOrigin.Begin);
wBitmap.SetSource(stream);
}
originalsize = wBitmap.ToByteArray().Length;
}
wb.SaveJpeg(fileStream, width, height, 0, 100);
}
else
{
wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 0, 100);
}
fileStream.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
希望這是有幫助的
您還沒有真正准確地描述您想要什么,您只是想在屏幕上將圖像渲染得更大/更小嗎? 或訪問調整大小圖像的像素值?
Image
元素並根據需要設置其Width
/ Height
。 該框架將負責為您擴展它。WriteableBitmap
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.