【问题标题】:How to get Photo stream after it has been manipulated to reduce size如何在经过操作以减小大小后获取照片流
【发布时间】:2012-01-26 10:05:45
【问题描述】:

使用 CameraCaptureTask 拍摄照片。

问题:当我在 ReduceSize() 函数中使用流 photostream = g_MS 时,流与 g_stream = e.ChosenPhoto 不同。

我想在 ReduceSize() 函数中缩小照片尺寸后得到与 g_stream = e.ChosenPhoto 相同的流。

stream g_stream; BitmapImage g_bmp; MemoryStream g_MS = new MemoryStream(); void task_Completed(object sender, Microsoft.Phone.Tasks.PhotoResult e) { try { if (e.TaskResult == TaskResult.OK) { g_bmp = new BitmapImage(); g_bmp.CreateOptions = BitmapCreateOptions.DelayCreation; g_bmp.SetSource(e.ChosenPhoto); g_stream = e.ChosenPhoto; ReduceSize(); } } catch (Exception ex) { } } private void ReduceSize() { string m_Filenm = "Testing.jpg"; WriteableBitmap wb = new WriteableBitmap(g_bmp); //--wb read byte into memorystream System.Windows.Media.Imaging.Extensions.SaveJpeg(wb, g_MS,800, 640, 0, 100); g_MS.Seek(0, SeekOrigin.Begin); stream photostream = g_MS; //------------------ Save //--save in the photo Library under Saved Photo Collection MediaLibrary ML = new MediaLibrary(); ML.SavePicture(m_Filenm, g_MS); }

【问题讨论】:

    标签: windows-phone-7


    【解决方案1】:

    只需从 ReduceSize 函数中检索流:

    stream g_stream;
    BitmapImage g_bmp;
    MemoryStream g_MS = new MemoryStream();
    
    void task_Completed(object sender, Microsoft.Phone.Tasks.PhotoResult e)
    {
        try
        {
            if (e.TaskResult == TaskResult.OK)
            {
                g_bmp = new BitmapImage();
    
                g_bmp.CreateOptions = BitmapCreateOptions.DelayCreation;
    
                g_bmp.SetSource(e.ChosenPhoto);
    
                g_stream = ReduceSize();
            }
        }
        catch (Exception ex)
        {
    
        }
    }
    
    private Stream ReduceSize()
    {
        string m_Filenm = "Testing.jpg";
    
        WriteableBitmap wb = new WriteableBitmap(g_bmp);
    
        //--wb read byte into memorystream
    
        System.Windows.Media.Imaging.Extensions.SaveJpeg(wb, g_MS, 800, 640, 0, 100);
    
        g_MS.Seek(0, SeekOrigin.Begin);
    
        stream photostream = g_MS;
    
        //------------------ Save
        //--save in the photo Library under Saved Photo Collection
    
        MediaLibrary ML = new MediaLibrary();
    
        ML.SavePicture(m_Filenm, g_MS);
    
        g_MS.Seek(0, SeekOrigin.Begin);
        return g_MS;
    }
    

    鉴于 MediaLibrary.SavePicture 不会关闭流,否则您必须先复制它。

    【讨论】:

    • 谢谢。如何在保存到 MediaLibrary 之前先获取照片流?
    • 我真的不明白你为什么需要它,所以它可能不是你期望的答案。您可以在将流保存到 MediaLibrary 之前制作流的副本,然后根据需要使用该副本。此链接说明如何复制流:stackoverflow.com/questions/4127051/…
    • 我这样做的原因是,与 300 KB 相比,这张减少照片的千字节减少到 9-16Kb。我需要使用此流进行上传。
    • 当然可以,但是为什么要在将图片保存到 MediaLibrary 之前检索流?实际上,为什么要保存它呢?无论如何,您还可以重构代码以从 ReduceSize 方法中删除 MediaLibrary 部分:调用 ReduceSize 以检索新流,上传它,然后调用 SavePicture 方法,将流保存到 MediaLibrary。
    • 试试你上面的方法。上传流后,它什么都不显示。
    猜你喜欢
    • 2012-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-20
    • 2012-06-14
    • 1970-01-01
    • 2012-07-31
    相关资源
    最近更新 更多