简体   繁体   English

可写位图宽高比保存图像

[英]writablebitmap aspect ratio save image

i'm read a image from PhotoChooserTask and have a stream of a photo. 我从PhotoChooserTask中读取了一张图片,并有一张照片流。 I have to reduce the size of image 我必须缩小图像的尺寸

i write this code 我写这段代码

            WriteableBitmap writeableBitmap = new WriteableBitmap(400, 400);
            writeableBitmap.LoadJpeg(stream);

            using (var isoFile = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (isoFile.FileExists("Myfile.jpg")) isoFile.DeleteFile("Myfile.jpg");
                using (var filestream = isoFile.CreateFile("Myfile.jpg"))
                {
                    writeableBitmap.SaveJpeg(filestream, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight, 0, 100);
                }
            }

this code not keep aspect ration of image. 此代码不保持图像的宽高比。

how to make? 怎么做?

First load the source image to writeableBitmap (without resizing). 首先将源图像加载到writeableBitmap(不调整大小)。

Then get the source width (PixelWidth) and full height (PixelHeight). 然后获取源宽度(PixelWidth)和完整高度(PixelHeight)。 Dividing PixelWidth with PixelHeight will give you the ratio. 用PixelHeight除以PixelWidth可得到比率。 You can use this value when resizing. 调整大小时可以使用此值。

So: 所以:

float aspectRatio = (float) writeableBitmap.PixelWidth / writeableBitmap.PixelHeight;

Then when saving just do 然后在保存时

writeableBitmap.SaveJpeg(filestream, ResizedWidthValue, (int) ResizedWidthValue / aspectRatio, 0, 100);

@ertay @ertay

i'm write this code 我正在写这段代码

using (var isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    WriteableBitmap wb = BitmapFactory.New(0, 0);
                    wb.FromStream(isolatedStorage.OpenFile("1.jpg", FileMode.Open, FileAccess.Read));

                    IsolatedStorageFileStream fileStream= isolatedStorage.CreateFile("1_thumb.jpg");

                    float aspectRatio = (float)wb.PixelWidth / wb.PixelHeight;

                    wb.SaveJpeg(fileStream, 200, (int) (200 / aspectRatio), 0, 100);

                    fileStream.Close();
                    wb = null;

                }

but wb.PixelWidth and wb.PixelHeight = 0!! 但是wb.PixelWidth和wb.PixelHeight = 0!

Why? 为什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM