将图片保存在媒体库中,其大小与手机在Windows手机中的屏幕尺寸相同

时间:2014-05-01 06:17:03

标签: c# windows-phone-7 windows-phone-8

在我的Windows手机应用程序中,我想将媒体库中的图片保存为与手机屏幕尺寸相同的尺寸 我这样做

        WriteableBitmap wb = new WriteableBitmap(480, 800);
        wb.Render(myitem, null);  // myItem is a UI Element
        wb.Invalidate(); 

        // create temporary image from it
        Image tmpImg = new Image();
        tmpImg.Source = wb;

        // this is required by WriteableBitmap 
        tmpImg.Measure(new Size(480, 800));
        tmpImg.Arrange(new Rect(0, 0, 100, 100));

        WriteableBitmap writeableBitmap = new WriteableBitmap(480, 800);
        writeableBitmap.Render(tmpImg, null);
        writeableBitmap.Invalidate(); 

        using (var mediaLibrary = new MediaLibrary())
        {
            using (var stream = new MemoryStream())
            { 
                writeableBitmap .SaveJpeg(stream, writeableBitmap .PixelWidth, writeableBitmap   
                                          .PixelHeight, 0, 100);
                stream.Seek(0, SeekOrigin.Begin);
                var picture = mediaLibrary.SavePicture("chk.jpg", stream); 
            }
        }


我得到这个 enter image description here

我希望这个enter image description here

1 个答案:

答案 0 :(得分:1)

如果您的问题是如何获取手机的物理像素大小,请尝试以下操作:

var frame = Window.Current.Content as Frame;
var width = frame.ActualWidth;
var height = frame.ActualHeight;

double resolutionscale;
#if WINDOWS_APP
    resolutionscale = ((int)Windows.Graphics.Display.DisplayInformation.GetForCurrentView().ResolutionScale / 100d);
#elif WINDOWS_PHONE_APP
    resolutionscale = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
    resolutionscale = Math.Pow(resolutionscale, 2d);
#elif WINDOWS_PHONE
    resolutionscale = ((int)Windows.Graphics.Display.DisplayProperties.ResolutionScale / 100d);
#endif    

var physicalSize = new Size(width * resolutionscale, height * resolutionscale);