如何在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;
}
答案 0 :(得分:0)
你还没有真正描述你想要什么,你只是想在屏幕上渲染图像更大/更小?或访问调整大小后的图像的像素值?
Image
元素并根据需要设置其Width
/ Height
。该框架将负责为您扩展它。WriteableBitmap
。答案 1 :(得分:0)
这是我缩小从中间文件中选择的图像或用相机拍摄的图像,希望它有所帮助。它保持纵横比。
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;
}
}
答案 2 :(得分:0)
这是我如何将图像尺寸减小到小于512 kb并将图像保存在隔离存储中, 警告:只是Windows手机的初学者,所以请承担我编码的方式
private void PhotoChooserTaskCompleted(object sender,PhotoResult e) { if(e.TaskResult!= TaskResult.OK)返回;
string Imgpath = "Rafiq.jpg";
SaveToIsolatedStorage(e.ChosenPhoto, Imgpath);
}
在此方法中,我传递的是e.choosen流及其文件名
并在隔离存储中保存已调整大小的图像(图像大小减少小于512 kb),希望这对您有所帮助,
private void SaveToIsolatedStorage(Stream imageStream,string fileName) { 尝试 { 使用(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);
}
}
希望这是helpFull