如何在操作照片流以减小尺寸后获取照片流

时间:2012-01-26 10:05:45

标签: windows-phone-7

使用CameraCaptureTask拍摄照片。

问题:当我在ReduceSize()函数中使用stream 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);


}

1 个答案:

答案 0 :(得分:0)

只需从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没有关闭流,否则你必须先复制它。