我需要从网络摄像头渲染实时视频。我想在每次新帧到达时更改Image对象的源。我创建了每次用新帧更新的WritableBitmap和在WritableBitmap上应用翻转变换作为源的TransformedBitmap。如果我为每个帧创建新的TransformedBitmap,我会看到视频输入,尽管内存占用增长非常快,导致GC非常频繁地收集内存。如果我正在尝试重用单个TransformedBitmap,我看不到渲染的更新,则始终显示第一帧。
如何在不增加内存占用量的情况下重用相同的TransformedBitmap对象?
以下是使用两个不同文件模拟网络摄像头的示例代码。
public partial class Window1 : Window
{
Thread m_thread;
BitmapImage source1;
BitmapImage source2;
byte[] data;
int stride;
WriteableBitmap bitmap;
TransformedBitmap tb;
public Window1()
{
InitializeComponent();
FileStream stream = new FileStream("1.png", FileMode.Open, FileAccess.Read);
source1 = new BitmapImage();
source1.BeginInit();
source1.StreamSource = stream;
source1.EndInit();
FileStream stream2 = new FileStream("2.png", FileMode.Open, FileAccess.Read);
source2 = new BitmapImage();
source2.BeginInit();
source2.StreamSource = stream2;
source2.EndInit();
stride = source1.PixelWidth * (source1.Format.BitsPerPixel / 8);
data = new byte[stride * source1.PixelHeight];
bitmap = new WriteableBitmap(source1.PixelWidth, source1.PixelHeight, source1.DpiX, source1.DpiY, source1.Format, source1.Palette);
WritePixels(source1, bitmap, data);
tb = new TransformedBitmap(bitmap, new ScaleTransform(-1, 1));
m_thread = new Thread(ThreadFunc);
m_thread.Start();
}
public void ThreadFunc()
{
int i = 0;
while (true)
{
Dispatcher.Invoke(new Action(() => {
BitmapSource source = (i % 2 == 0) ? source2 : source1;
WritePixels(source, bitmap, data);
image1.Source = tb;
}));
++i;
Thread.Sleep(100);
}
}
public void WritePixels(BitmapSource source, WriteableBitmap target, byte[] data)
{
int stride = source.PixelWidth * (source.Format.BitsPerPixel / 8);
source.CopyPixels(data, stride, 0);
target.WritePixels(new Int32Rect(0, 0, source.PixelWidth, source.PixelHeight), data, stride, 0);
}
}
答案 0 :(得分:3)
我明白了,问题是由于TransformedBitmap是相同的,所以如果我使用具有相同源的两个不同的TransformedBitmap对象,那么Image对象可能无法识别更改,然后写完新内容我切换它运行的TransformedBitmap,我有新的内容呈现。
所以代码是:
public partial class MainWindow : Window
{
Thread m_thread;
BitmapImage source1;
BitmapImage source2;
byte[] data;
int stride;
WriteableBitmap bitmap1;
TransformedBitmap tb1;
TransformedBitmap tb2;
public MainWindow()
{
InitializeComponent();
FileStream stream = new FileStream("1.jpg", FileMode.Open, FileAccess.Read);
source1 = new BitmapImage();
source1.BeginInit();
source1.StreamSource = stream;
source1.EndInit();
FileStream stream2 = new FileStream("2.jpg", FileMode.Open, FileAccess.Read);
source2 = new BitmapImage();
source2.BeginInit();
source2.StreamSource = stream2;
source2.EndInit();
stride = source1.PixelWidth * (source1.Format.BitsPerPixel / 8);
data = new byte[stride * source1.PixelHeight];
bitmap1 = new WriteableBitmap(source1.PixelWidth, source1.PixelHeight, source1.DpiX, source1.DpiY, source1.Format, source1.Palette);
WritePixels(source1, bitmap1, data);
tb1 = new TransformedBitmap(bitmap1, new ScaleTransform(-1, -1));
tb2 = new TransformedBitmap(bitmap1, new ScaleTransform(-1, -1));
m_thread = new Thread(ThreadFunc);
m_thread.Start();
}
public void ThreadFunc()
{
int i = 0;
while (true)
{
Dispatcher.Invoke(new Action(() => {
BitmapSource source = (i % 2 == 0) ? source2 : source1;
WritePixels(source, bitmap1, data);
// this is the trick we have to set different TransformedBitmap as
// Image.Source
// so since we don't want to create new one each time we just
// switch between two
image1.Source = (i % 2 == 0) ? tb1 : tb2;
}));
++i;
Thread.Sleep(100);
}
}
public void WritePixels(BitmapSource source, WriteableBitmap target, byte[] data)
{
int stride = source.PixelWidth * (source.Format.BitsPerPixel / 8);
source.CopyPixels(data, stride, 0);
target.WritePixels(new Int32Rect(0, 0, source.PixelWidth, source.PixelHeight), data, stride, 0);
}
}