这一个:
BitmapSource originalImage;
byte[] _originalPixels;
_originalPixels = new byte[(int) originalImage.Width*(int) originalImage.Height*4];
originalImage.CopyPixels(_originalPixels, 4*(int) originalImage.Width, 0);
在应用过滤器之前复制图像字节,这并不奇怪。
如何获取已应用效果的字节?
如何以编程方式将着色器效果应用于byte []或某种低级像素结构数组?
答案 0 :(得分:3)
助手:
public static class ImageRenderingHelper
{
public static BitmapSource RenderToBitmap(FrameworkElement target)
{
int actualWidth = (int)target.ActualWidth;
int actualHeight = (int)target.ActualHeight;
Rect boundary = VisualTreeHelper.GetDescendantBounds(target);
RenderTargetBitmap renderBitmap = new RenderTargetBitmap(actualWidth, actualHeight, 96, 96, PixelFormats.Pbgra32);
DrawingVisual drawingVisual = new DrawingVisual();
using (DrawingContext context = drawingVisual.RenderOpen())
{
VisualBrush visualBrush = new VisualBrush(target);
context.DrawRectangle(visualBrush, null, new Rect(new Point(), boundary.Size));
}
renderBitmap.Render(drawingVisual);
return renderBitmap;
}
private static void Arrange(UIElement element, int width, int height)
{
element.Measure(new Size(width, height));
element.Arrange(new Rect(0, 0, width, height));
element.UpdateLayout();
}
public static byte[] RenderImageWithEffects(ImageSource image, IEnumerable<ShaderEffect> effects)
{
effects = effects.Reverse();
Grid root = new Grid();
Arrange(root, (int)image.Width, (int)image.Height);
Grid current = root;
foreach (var shaderEffect in effects)
{
var effect = new Grid();
Arrange(effect, (int)image.Width, (int)image.Height);
effect.Effect = shaderEffect;
current.Children.Add(effect);
current = effect;
}
Image img = new Image();
img.Source = image;
Arrange(img, (int)image.Width, (int)image.Height);
current.Children.Add(img);
BitmapSource bs = RenderToBitmap(root);
byte[] buffer = new byte[(int)bs.Width * (int)bs.Height * 4];
bs.CopyPixels(buffer, 4 * (int)bs.Width, 0);
return buffer;
}
public static byte[] RenderImageWithEffects(string imagePath, IEnumerable<ShaderEffect> effects)
{
BitmapImage bmp = new BitmapImage(new Uri(imagePath, UriKind.Absolute));
return RenderImageWithEffects(bmp, effects);
}
}
答案 1 :(得分:2)
如何将图片渲染为RenderTargetBitmap
并使用CopyPixels
上的RenderTargetBitmap
获取像素?这有帮助吗?