我将如何管理WPF中的逐像素渲染(例如,对于光线追踪器)?我最初的猜测是创建一个BitmapImage,修改缓冲区,并显示在一个Image控件,但我不知道如何创建一个(创建方法需要一个非管理内存块)在WPF中绘制像素
回答
我强烈建议对前两个建议。假设您使用3.5 SP1,The WriteableBitmap class提供了您需要的内容。在SP1之前,除非你诉诸一些严重的欺骗,否则在WPF中这个问题没有很好的答案。
你可以放置1X1矩形对象到画布上
private void AddPixel(double x, double y)
{
Rectangle rec = new Rectangle();
Canvas.SetTop(rec, y);
Canvas.SetLeft(rec, x);
rec.Width = 1;
rec.Height = 1;
rec.Fill = new SolidColorBrush(Colors.Red);
myCanvas.Children.Add(rec);
}
这应该是相当接近你想要
您可以添加小rects如果你想要的,但每一类又是一个FrameworkElement的是什么,所以它可能是一个有点重量级。另一种选择是创建自己DrawingVisual,借鉴它,使它然后坚持它的图像:
private void DrawRubbish()
{
DrawingVisual dv = new DrawingVisual();
using (DrawingContext dc = dv.RenderOpen())
{
Random rand = new Random();
for (int i = 0; i < 200; i++)
dc.DrawRectangle(Brushes.Red, null, new Rect(rand.NextDouble() * 200, rand.NextDouble() * 200, 1, 1));
dc.Close();
}
RenderTargetBitmap rtb = new RenderTargetBitmap(200, 200, 96, 96, PixelFormats.Pbgra32);
rtb.Render(dv);
Image img = new Image();
img.Source = rtb;
MainGrid.Children.Add(img);
}
的最佳解决方案。像魅力一样工作。只有一件小事: dc.Close();不需要 。使用调用Dispose(),它和Close()一样。我检查了WPF源代码:-) – 2016-01-03 18:12:33
如果您正在考虑做类似光线追踪的事情,您需要绘制很多点,您可能需要直接在位图的内存空间中绘制点,而不是通过抽象层。这段代码会给你提供更好的渲染时间,但是它的代价是需要“不安全”的代码,这可能或可能不适合你。
unsafe
{
System.Drawing.Point point = new System.Drawing.Point(320, 240);
IntPtr hBmp;
Bitmap bmp = new Bitmap(640, 480);
Rectangle lockRegion = new Rectangle(0, 0, 640, 480);
BitmapData data = bmp.LockBits(lockRegion, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
byte* p;
p = (byte*)data.Scan0 + (point.Y * data.Stride) + (point.X * 3);
p[0] = 0; //B pixel
p[1] = 255; //G pixel
p[2] = 255; //R pixel
bmp.UnlockBits(data);
//Convert the bitmap to BitmapSource for use with WPF controls
hBmp = bmp.GetHbitmap();
Canvas.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hbmpCanvas, IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
Canvas.Source.Freeze();
DeleteObject(hBmp); //Clean up original bitmap
}
要清理HBITMAP你需要声明这附近你的类文件的顶部:
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
另外请注意,您将需要设置项目属性,允许不安全的代码。您可以通过右键单击解决方案资源管理器中的项目并选择属性来完成此操作。转到生成选项卡。选中“允许不安全的代码”框。另外我相信你需要添加一个对System.Drawing的引用。
非常感谢这个解决方案。 – 2010-10-13 12:32:15
这是一个使用WritableBitmap的方法。
public void DrawRectangle(WriteableBitmap writeableBitmap, int left, int top, int width, int height, Color color)
{
// Compute the pixel's color
int colorData = color.R << 16; // R
colorData |= color.G << 8; // G
colorData |= color.B << 0; // B
int bpp = writeableBitmap.Format.BitsPerPixel/8;
unsafe
{
for (int y = 0; y < height; y++)
{
// Get a pointer to the back buffer
int pBackBuffer = (int)writeableBitmap.BackBuffer;
// Find the address of the pixel to draw
pBackBuffer += (top + y) * writeableBitmap.BackBufferStride;
pBackBuffer += left * bpp;
for (int x = 0; x < width; x++)
{
// Assign the color data to the pixel
*((int*)pBackBuffer) = colorData;
// Increment the address of the pixel to draw
pBackBuffer += bpp;
}
}
}
writeableBitmap.AddDirtyRect(new Int32Rect(left, top, width, height));
}
,并使用它:
private WriteableBitmap bitmap = new WriteableBitmap(1100, 1100, 96d, 96d, PixelFormats.Bgr24, null);
private void Button_Click(object sender, RoutedEventArgs e)
{
int size = 10;
Random rnd = new Random(DateTime.Now.Millisecond);
bitmap.Lock(); // Lock() and Unlock() could be moved to the DrawRectangle() method. Just do some performance tests.
for (int y = 0; y < 99; y++)
{
for (int x = 0; x < 99; x++)
{
byte colR = (byte)rnd.Next(256);
byte colG = (byte)rnd.Next(256);
byte colB = (byte)rnd.Next(256);
DrawRectangle(bitmap, (size + 1) * x, (size + 1) * y, size, size, Color.FromRgb(colR, colG, colB));
}
}
bitmap.Unlock(); // Lock() and Unlock() could be moved to the DrawRectangle() method. Just do some performance tests.
image.Source = bitmap; // This should be done only once
}
- 1. 如何在WPF中绘制像素
- 2. WPF中的快速像素绘制
- 3. 绘制像素
- 4. 在Java中绘制像素图像
- 5. 如何以像素为单位在WPF中绘制
- 6. 在UI元素顶部绘制WPF
- 7. 如何绘制坐标[0; 1]而不是WPF中的像素?
- 8. 在Quartz中绘制单个像素
- 9. 如何在XNA中绘制2D像素?
- 10. 在JavaFX中绘制子像素?
- 11. 在OpenGL中绘制单个像素
- 12. 在OpenGL中绘制大量像素
- 13. 在JFrame Swing中绘制像素
- 14. 在R中绘制体素图像
- 15. 如何在SDL 2.0中绘制像素?
- 16. 在wpf中绘制图表
- 17. 在WPF中绘制数字
- 18. 在WPF中绘制3D线
- 19. 在WPF中绘制地图
- 20. 使用XCode绘制像素
- 21. 绘制像素 - Objective-C/Cocoa
- 22. 按像素绘制pyglet(python)
- 23. 像素绘制算法
- 24. 如何在WPF中的1像素厚的画布上绘制线条
- 25. 我怎样才能让DrawGeometry在WPF中绘制设备像素线?
- 26. iOS中绘制像素阵列
- 27. 在SDL_Surface像素上绘制矩形
- 28. 在画布上绘制像素椭圆
- 29. 在QPixmap上绘制的奇怪像素
- 30. VB.NET绘制图像(组像素)
WriteableBitmap类自.NET 3.0起可用,这是WPF的第一个版本。你不需要.NET 3.5来使用这个类。 – 2011-09-19 01:27:12