调整位图图像 wp7 的大小

发布于 2024-11-29 09:01:09 字数 298 浏览 5 评论 0原文

如何让它在 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;
}

How to get this working on 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

此生挚爱伱 2024-12-06 09:01:09

这就是我缩小从媒体库中选择的或用相机拍摄的图像的方法,希望它有所帮助。它保持纵横比。

    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;
        }
    }

This is how I scale down an image selected from the medialibrary or taken with the camera, hope it helps. It keeps the aspect ratio.

    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;
        }
    }
初心 2024-12-06 09:01:09

您还没有真正准确地描述您想要什么,您只是想在屏幕上渲染图像更大/更小吗?或访问调整大小的图像的像素值?

  1. 要将位图图像渲染为不同的尺寸/比例,您只需使用 Image 元素并根据需要设置其 Width / Height 即可。该框架将负责为您扩展它。
  2. 如果需要获取缩放结果的像素值,请根据(1)中描述的缩放图像构造一个WriteableBitmap

You haven't really described exactly what you want, do you just want to render the image bigger / smaller on the screen? or access the pixel values of a resized image?

  1. To render an bitmap image to a different size / scale, you simple use an Image element and set its Width / Height as desired. The framework will take care of scaling it for you.
  2. If you need to obtain the pixel values of the scaled result, construct a WriteableBitmap from the scaled image described in (1).
念﹏祤嫣 2024-12-06 09:01:09

这就是我如何将图像大小减小到小于 512 kb 并将图像保存在独立存储中,
注意:只是 Windows Phone 的初学者,所以请忍受我编码的方式

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);
        }
    }

希望这是有帮助的

This is How i have reduced the Image size that too less than 512 kb and saving image in Isolated storage,
Caution: Just a beginner in windows phone so pls bear the way i code

private void PhotoChooserTaskCompleted(object sender, PhotoResult e)
{
if (e.TaskResult != TaskResult.OK) return;

  string Imgpath = "Rafiq.jpg";
                SaveToIsolatedStorage(e.ChosenPhoto, Imgpath);
}

Here in this Method i m passing the stream that is e.choosen and its file name

and saving resized image (images size reducing less than 512 kb) in Isolated Storage, hope this helps you,

private void SaveToIsolatedStorage(Stream imageStream, string fileName)
{
try
{
using (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);
        }
    }

hope this was helpFull

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文