Windows 8 如何创建单色位图以显示给定的色调?
我有一个基于特定颜色创建图像的要求。颜色会有所不同,输出图像的大小也会有所不同。我想创建位图并将其保存到应用程序的临时文件夹中。我该怎么做Windows 8 如何创建单色位图以显示给定的色调?,windows-8,windows-store-apps,winrt-xaml,Windows 8,Windows Store Apps,Winrt Xaml,我有一个基于特定颜色创建图像的要求。颜色会有所不同,输出图像的大小也会有所不同。我想创建位图并将其保存到应用程序的临时文件夹中。我该怎么做 我最初的需求来自一个颜色列表,并在UI中提供了一个颜色示例。如果图像的大小是可变的,那么我可以为某些场景创建它们,比如搜索窗格中的结果建议。这并不容易。但它都被包装在这个单一的方法中供您使用。我希望有帮助。无论如何,下面是基于给定颜色和大小创建位图的代码: private async System.Threading.Tasks.Task<Window
我最初的需求来自一个颜色列表,并在UI中提供了一个颜色示例。如果图像的大小是可变的,那么我可以为某些场景创建它们,比如搜索窗格中的结果建议。这并不容易。但它都被包装在这个单一的方法中供您使用。我希望有帮助。无论如何,下面是基于给定颜色和大小创建位图的代码:
private async System.Threading.Tasks.Task<Windows.Storage.StorageFile> CreateThumb(Windows.UI.Color color, Windows.Foundation.Size size)
{
// create colored bitmap
var _Bitmap = new Windows.UI.Xaml.Media.Imaging.WriteableBitmap((int)size.Width, (int)size.Height);
byte[] _Pixels = new byte[4 * _Bitmap.PixelWidth * _Bitmap.PixelHeight];
for (int i = 0; i < _Pixels.Length; i += 4)
{
_Pixels[i + 0] = color.B;
_Pixels[i + 1] = color.G;
_Pixels[i + 2] = color.R;
_Pixels[i + 3] = color.A;
}
// update bitmap data
// using System.Runtime.InteropServices.WindowsRuntime;
using (var _Stream = _Bitmap.PixelBuffer.AsStream())
{
_Stream.Seek(0, SeekOrigin.Begin);
_Stream.Write(_Pixels, 0, _Pixels.Length);
_Bitmap.Invalidate();
}
// determine destination
var _Folder = Windows.Storage.ApplicationData.Current.TemporaryFolder;
var _Name = color.ToString().TrimStart('#') + ".png";
// use existing if already
Windows.Storage.StorageFile _File;
try { return await _Folder.GetFileAsync(_Name); }
catch { /* do nothing; not found */ }
_File = await _Folder.CreateFileAsync(_Name, Windows.Storage.CreationCollisionOption.ReplaceExisting);
// extract stream to write
// using System.Runtime.InteropServices.WindowsRuntime;
using (var _Stream = _Bitmap.PixelBuffer.AsStream())
{
_Pixels = new byte[(uint)_Stream.Length];
await _Stream.ReadAsync(_Pixels, 0, _Pixels.Length);
}
// write file
using (var _WriteStream = await _File.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite))
{
var _Encoder = await Windows.Graphics.Imaging.BitmapEncoder
.CreateAsync(Windows.Graphics.Imaging.BitmapEncoder.PngEncoderId, _WriteStream);
_Encoder.SetPixelData(Windows.Graphics.Imaging.BitmapPixelFormat.Bgra8,
Windows.Graphics.Imaging.BitmapAlphaMode.Premultiplied,
(uint)_Bitmap.PixelWidth, (uint)_Bitmap.PixelHeight, 96, 96, _Pixels);
await _Encoder.FlushAsync();
using (var outputStream = _WriteStream.GetOutputStreamAt(0))
await outputStream.FlushAsync();
}
return _File;
}
<代码>私有异步System.Threading.Tasks.Task CreateThumb(WiDOWS.UI.颜色、窗口、基础、大小)
{
//创建彩色位图
var_Bitmap=newwindows.UI.Xaml.Media.Imaging.WriteableBitmap((int)size.Width,(int)size.Height);
字节[]\u像素=新字节[4*\u Bitmap.PixelWidth*\u Bitmap.PixelHeight];
对于(int i=0;i<_像素。长度;i+=4)
{
_像素[i+0]=color.B;
_像素[i+1]=颜色.G;
_像素[i+2]=color.R;
_像素[i+3]=颜色A;
}
//更新位图数据
//使用System.Runtime.InteropServices.WindowsRuntime;
使用(var _Stream=_Bitmap.PixelBuffer.AsStream())
{
_Stream.Seek(0,SeekOrigin.Begin);
_Stream.Write(_Pixels,0,_Pixels.Length);
_Bitmap.Invalidate();
}
//确定目的地
var\u Folder=Windows.Storage.ApplicationData.Current.temporary文件夹;
var_Name=color.ToString().TrimStart('#')+“.png”;
//如果已经存在,请使用现有的
Windows.Storage.StorageFile\u文件;
请尝试{return await}Folder.GetFileAsync(_Name);}
捕获{/*不执行任何操作;未找到*/}
_File=wait _Folder.CreateFileAsync(_Name,Windows.Storage.CreationCollisionOption.ReplaceExisting);
//提取要写入的流
//使用System.Runtime.InteropServices.WindowsRuntime;
使用(var _Stream=_Bitmap.PixelBuffer.AsStream())
{
_像素=新字节[(uint)_Stream.Length];
wait _Stream.ReadAsync(_Pixels,0,_Pixels.Length);
}
//写入文件
使用(var\u WriteStream=await\u File.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite))
{
var_Encoder=等待Windows.Graphics.Imaging.BitmapEncoder
.CreateAsync(Windows.Graphics.Imaging.BitmapEncoder.PngEncoderId,_WriteStream);
_编码器.SetPixelData(Windows.Graphics.Imaging.BitmapPixelFormat.Bgra8,
Windows.Graphics.Imaging.BitmapAlphaMode.Premultiply,
(uint)_Bitmap.PixelWidth,(uint)_Bitmap.PixelHeight,96,96,_像素);
wait_Encoder.FlushAsync();
使用(var outputStream=_WriteStream.getOutputstreeamat(0))
等待outputStream.FlushAsync();
}
返回文件;
}
祝你好运