问题就在这里。我想从本地驱动器打开一个文件,然后将其放入 WritableBitmap 中,以便我可以编辑它。但问题是,我无法从 Uri 或类似的东西创建 WritableBitmap。我也知道如何将文件打开到 BitmapImage 中,但我不知道如何将文件打开为 WritableBitmap。有没有办法直接将文件打开为 WritableBitmap,如果没有,是否有办法将 BitmapImage 转换为 WritableBitmap?多谢你们。
我正在开发一个silverlight应用程序,我的所有图标都是PNG.
所有这些图标的颜色都是黑色,或者更确切地说是黑色到灰色,具体取决于alpha值.每个PNG都有透明的背景.
在我的应用程序中,我想做一个逐像素颜色从黑色或灰色变化,让我们说红色(黑色之前)或浅红色(灰色之前).
我认识到,遍历每个像素的字节,每个全黑像素的alpha值为255.灰色的那些像素值在1到254之间.α值为0似乎是透明的.黑色的RGB值均为0.
我的想法是,我改变了每个像素的颜色,但保留了alpha值,以便PNG保持原始的外观.
为此,我写了一个小函数,看起来像这样:
private static WriteableBitmap ColorChange(WriteableBitmap wbmi, System.Windows.Media.Color color)
{
for (int pixel = 0; pixel < wbmi.Pixels.Length; pixel++)
{
if (wbmi.Pixels[pixel] != 0)
{
byte[] colorArray = BitConverter.GetBytes(wbmi.Pixels[pixel]);
byte blue = colorArray[0];
byte green = colorArray[1];
byte red = colorArray[2];
byte alpha = colorArray[3];
if (alpha > 0)
{
//HERE, I change the color, but keep the alpha
//
wbmi.Pixels[pixel] = Gui.Colors.ToInt.Get(color,alpha);
colorArray = BitConverter.GetBytes(wbmi.Pixels[pixel]);
blue = colorArray[0];
green = colorArray[1];
red = colorArray[2]; …Run Code Online (Sandbox Code Playgroud) 我正在用WPF(C#)编写一个应用程序,它对一组Bitmaps进行长时间的操作.为了保持我的应用程序响应,我决定使用另一个线程来执行位图操作并报告主UI线程中进度条的进度.我认为BackgroundWorker会为我做任何事情,但看起来不会那么容易.
我有以下代码:
public class ImageProcessor
{
public Collection<WriteableBitmap> Pictures { get; private set; }
private BackgroundWorker _worker = new BackgroundWorker();
public ImageProcessor()
{
_worker.DoWork += DoWork;
}
public void DoLotsOfOperations()
{
_worker.RunWorkerAsync();
}
private void DoWork(object sender, DoWorkEventArgs e)
{
// operations on Pictures collection
}
}
Run Code Online (Sandbox Code Playgroud)
在运行时,我使用标准打开文件对话框将图像加载到Pictures集合中,然后调用DoLotsOfOperations()方法.但是当我尝试访问单个位图的任何属性时,我得到InvalidOperationException:"调用线程无法访问该对象,因为不同的线程拥有它".
这是真的 - 我加载了位图并在UI线程中填充了集合,我尝试在另一个线程中读取集合元素.所以我尝试了不同的方法:
那么如何在另一个线程中访问位图的数据(最好是使用BackgroundWorker)?
我不知道,也许我的整个方法都是错的.我想要实现的一般想法是:
在此先感谢您的帮助.
你知道任何库提供使用WPF WriteableBitmap和理想的BackBuffer绘制简单形状(线条和可选的其他形状)的方法吗?我知道有一个适用于Silverlight的WriteableBitmapEx项目,但WPF是否等效?
我创建了一个 Gray16 格式的 WriteableBitmap。我想将此 WriteableBitmap 调整为我已知的尺寸,保留像素格式(Gray16)。
是否有人致力于调整可写位图的大小。请帮我。
我还搜索了互联网并找到了http://writeablebitmapex.codeplex.com/但这通过一个assebmly 引用错误。
请帮我。
我目前有一个writeablebitmap图像和带有绘图的画布,我想将图像发送给对等.为了减少带宽,我想将canvas转换为writeablebitmap,因此我可以将两个图像blit到一个新的writeablebitmap.问题是我无法找到转换画布的好方法.因此,我想问一下是否有直接的方法将画布转换为writeablebitmap类.
我在WinRT项目中使用WriteableBitmapEx.我将图像加载到用户图片库的WriteableBitmap中.但是,我不能立即写入该图像,如果我这样做,它将被图像本身覆盖(看起来它是异步加载图像然后它覆盖我的绘图在它上面).我不知道如何阻止它(我尝试在SetSource上使用Await,但这不是Async方法).
我已经使用了"Await Task.Delay(1000)"并且有效,但它看起来很hacky因为1000ms可能或者可能没有足够的时间..我希望它等到位图加载后再继续.
任何人都可以看出我做错了什么或建议我如何确保在进行任何处理之前从图片库加载WriteableBitmap?这是我创建的示例代码段:
Dim file = Await picker.PickSingleFileAsync
If file Is Nothing Then
Exit Sub
End If
Dim wbm As New WriteableBitmap(1, 1)
wbm.SetSource(Await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
' If I don't have this, the DrawLine will not show up, if I do, it will.
Await Task.Delay(1000)
wbm.DrawLine(1, 1, 255, 255, Colors.Green)
wbm.Invalidate()
ImageMain.Source = wbm
Run Code Online (Sandbox Code Playgroud) filepicker writeablebitmap async-await writeablebitmapex windows-runtime
反正有没有将 WriteableBitmap 转换为字节数组?如果有办法从中获取它,我也将可写位图分配给 System.Windows.Controls.Image 源。我试过了,但在 FromHBitmap 上遇到了一般的 GDI 异常。
System.Drawing.Image img = System.Drawing.Image.FromHbitmap(wb.BackBuffer);
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
myarray = ms.ToArray();
Run Code Online (Sandbox Code Playgroud) 我当前的代码是将一个小的 Pbgra32 位图 blit'ing 到一个更大的 Pbgra32 位图上。工作正常。我现在想做的就是让那个较小的部分透明。为此,在 blit 之前,我将较小的值传递给一种方法,该方法应通过保留 RGB 值来编辑每个像素,同时将 0x7F 写入每个像素的 A 值。
然而,我得到的不是 50% 透明的图像,而是一个灰色的方块。我究竟做错了什么?
private void MakeTransparent(ref WriteableBitmap bmp)
{
int width = bmp.PixelWidth;
int height = bmp.PixelHeight;
int stride = bmp.BackBufferStride;
int bytesPerPixel = (bmp.Format.BitsPerPixel + 7)/8;
unsafe
{
bmp.Lock();
byte* pImgData = (byte*) bmp.BackBuffer;
int cRowStart = 0;
int cColStart = 0;
for (int row = 0; row < height; row++)
{
cColStart = cRowStart;
for (int col = 0; col < width; col++) …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用WriteableBitmapEx从我的硬盘驱动器上的Image创建一个WriteableBitmap.我在添加nuget包后复制了官方页面中的代码:
WriteableBitmap writeableBmp = new WriteableBitmap(0, 0).FromResource("Data/flower2.png");
Run Code Online (Sandbox Code Playgroud)
但我得到了错误
'System.Windows.Media.Imaging.WriteableBitmap'不包含带有2个参数的构造函数
我添加了PresentationCore.dll,因为我在控制台应用程序中被告知WriteableBitmapEx? 以及System.Windows.Media.Imaging的using语句.
writeablebitmap ×10
c# ×8
wpf ×4
alpha ×1
async-await ×1
asynchronous ×1
bitmap ×1
colors ×1
filepicker ×1
pixel ×1
silverlight ×1