在 WIndows phone 8.1 中将多边形对象保存为位图图像

Saving a polygon object into a bitmapimage in WIndows phone 8.1

我正在开发 windows phone 8.1 应用程序,我需要将多边形对象转换为图像并最终将其保存为 png 文件。到现在为止,我创建了一个具有各种属性的多边形对象。现在我对其他部分一无所知。

        pol.Opacity = 0.5;
        System.Windows.Point Point1 = new System.Windows.Point(10, 200);
        System.Windows.Point Point2 = new System.Windows.Point(60, 140);
        System.Windows.Point Point3 = new System.Windows.Point(130, 140);
        System.Windows.Point Point4 = new System.Windows.Point(180, 200);
        System.Windows.Point Point5 = new System.Windows.Point(130, 260);
        System.Windows.Point Point6 = new System.Windows.Point(60, 260);
        PointCollection myPointCollection = new PointCollection();
        myPointCollection.Add(Point1);
        myPointCollection.Add(Point2);
        myPointCollection.Add(Point3);
        myPointCollection.Add(Point4);
        myPointCollection.Add(Point5);
        myPointCollection.Add(Point6);
        pol.Points = myPointCollection;
        var imageBrush = new ImageBrush();
        imageBrush.ImageSource = image.Source;
        pol.Fill = imageBrush;
        pol.Height = image.Height;
        pol.MaxHeight = image.Height;
        pol.MaxWidth = image.Width;
        pol.Width = image.Width;
        pol.Stroke = new SolidColorBrush(Colors.Red);
        pol.StrokeThickness = 2;
        pol.Margin = image.Margin;

您可以使用 WritableBitmap class 来实现这一点。我在Silverlight上有一个类似的post,你可以参考:How to Crop an Image based on a Shape or Path control?。希望能有所帮助,至少给出一些基本概念。如果您需要进一步的帮助,请告诉我。

要将形状保存为 PNG,您可以使用以下代码片段:

       WriteableBitmap bmp = GetAsWritableBitmap();
       using (var mediaLibrary = new MediaLibrary())
       {
           using (var stream = new MemoryStream())
           {
               var fileName = string.Format("Gs{0}.jpg", Guid.NewGuid());
               bmp.SaveJpeg(stream, bmp.PixelWidth, bmp.PixelHeight, 0, 100);
               stream.Seek(0, SeekOrigin.Begin);
               var picture = mediaLibrary.SavePicture(fileName, stream);
               if (picture.Name.Contains(fileName)) return true;
           }
       }

希望对您有所帮助。