windows phone,如何使用WriteableBitmap和UserControl渲染图像?

时间:2013-09-03 04:33:45

标签: silverlight windows-phone

这是我的代码:

// create image1
var tileControl1 = new FlipCycleTileSmallControl("");
tileControl1.Measure(newSize(159, 159));
tileControl1.Arrange(newRect(0d, 0d, 159, 159));
var writeableBitmap1 = new System.Windows.Media.Imaging.WriteableBitmap(tileControl1, null);
image1.Source = writeableBitmap1;

// create image2 
var tileControl2 = new FlipCycleTileMediumControl("");
tileControl2.Measure(newSize(336, 336));
tileControl2.Arrange(newRect(0d, 0d, 336, 336));
var writeableBitmap2 = new System.Windows.Media.Imaging.WriteableBitmap(tileControl2, null);
image2.Source = writeableBitmap2;

// create image3
var tileControl3 = new FlipCycleTileMediumControl("");
tileControl3.Measure(newSize(691, 336));
tileControl3.Arrange(newRect(0d, 0d, 691, 336));
var writeableBitmap3 = new System.Windows.Media.Imaging.WriteableBitmap(tileControl3, null);
image3.Source = writeableBitmap3;

使用此代码,我想从

创建一个图像
  

用户控件(FlipCycleTileSmallControl,FlipCycleTileMediumControl,FlipCycleTileMediumControl)

并将其保存到IsolatedStorage。

Image1很好,但是image2和image3在黑色背景下表现不佳。

我该怎么办?

1 个答案:

答案 0 :(得分:0)

我不确定你是如何从你提供的代码得到任何结果的,但它类似于我使用的一些类似的代码。在下面的示例中,我采用用户控件并将图像保存到隔离存储。稍后我可以很容易地引用这条路径。

如果这不是您所希望的,我希望它能激发您的解决方案。

public static void SaveToIsolatedStorage(this UserControl control, string filename, Size size = new Size())
    {

        var controlWidth = (int) size.Width;
        var controlHeight = (int) size.Height;

        control.Height = controlHeight;
        control.Width = controlWidth;

        control.Arrange(new Rect(0, 0, controlWidth, controlHeight));

        // Create writeable bitmap to save as image
        var wb = new WriteableBitmap(controlWidth, controlHeight);

        // Position child objects
        control.Arrange(new Rect(0, 0, controlWidth, controlHeight));

        // Render element to bitmap
        wb.Render(control, new CompositeTransform());

        // Draw
        wb.Invalidate();

        // At this point you can do what you want with the output of writeable bitmap
        // I recommend saving to isolated storage, the access it locally.

         // Save to isolated storage
        using (var isfs = new IsolatedStorageFileStream(string.Format("/Shared/ShellContent/{0}.jpg", filename), FileMode.Create, IsolatedStorageFile.GetUserStoreForApplication()))
        {
            wb.SaveJpeg(isfs, controlWidth, controlHeight, 0, 100);
        }

    }