Stu*_*ton 6 c# memorystream bitmapimage uwp
我有一个使用转换器在XAML加载的图像。而不是负载这一形象再次,我要采取的图像,并找到主色,以便能够使用网页上的其他图形。到目前为止,我有这个:
var himage = (BitmapImage)image_home.Source;
using (var stream = await himage.OpenReadAsync()) //**can't open himage this way**
{
//Create a decoder for the image
var decoder = await BitmapDecoder.CreateAsync(stream);
//Create a transform to get a 1x1 image
var myTransform = new BitmapTransform { ScaledHeight = 1, ScaledWidth = 1 };
//Get the pixel provider
var pixels = await decoder.GetPixelDataAsync(
BitmapPixelFormat.Rgba8,
BitmapAlphaMode.Ignore,
myTransform,
ExifOrientationMode.IgnoreExifOrientation,
ColorManagementMode.DoNotColorManage);
//Get the bytes of the 1x1 scaled image
var bytes = pixels.DetachPixelData();
//read the color
var myDominantColor = Color.FromArgb(255, bytes[0], bytes[1], bytes[2]);
}
Run Code Online (Sandbox Code Playgroud)
很显然,我可以用OpenReadAsync无法打开的BitmapImage画佳,有什么事情,我需要在那里做才能够实现这一目标?
BitmapDecoder需要RandomAccessStream对象来创建新实例。除非您知道原始来源,否则BitmapImage可能无法直接提取。RandomAccessStream根据您的评论,您将图像 Uri 绑定到图像控件,因此您可以知道原始来源,并且可以按类从属性中获取图像, 不需要RandomAccessStream再次加载图像。代码如下:BitmapImageUriSourceRandomAccessStreamReference
var himage = (BitmapImage)image_home.Source;\n RandomAccessStreamReference random = RandomAccessStreamReference.CreateFromUri(himage.UriSour\xe2\x80\x8c\xe2\x80\x8bce);\n\n using (IRandomAccessStream stream = await random.OpenReadAsync()) \n {\n //Create a decoder for the image\n var decoder = await BitmapDecoder.CreateAsync(stream);\n ...\n //read the color \n var myDominantColor = Color.FromArgb(255, bytes[0], bytes[1], bytes[2]);\n }\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
2854 次 |
| 最近记录: |