我将接收一些将存储在字节数组中的原始数据,其中每2个字节是一个像素值(16位/ px).首先,该阵列将包含100x100*2字节(足以容纳100x100像素的图像).我想在窗体窗口中显示此数据.最后,我想用新数据刷新图像,使其看起来像一个视频流.不需要严格的帧速率.如何才能做到这一点?C#中的任何代码示例?
编辑: 经过一些建议和审查数十个类似的问题后,我仍然无法得到这个.以下是我想要做的一般概念,但图像不会显示在表单的图片框中.我的实施有什么特别的错误以及如何解决它?
// array of data I collected
byte[] dataArray = new byte[100 * 100 * 2];
//create a pointer to the data
IntPtr hglobal = Marshal.AllocHGlobal(100 * 100 * 2);
// copy my array to global
Marshal.Copy(dataArray, 0, hglobal, dataArray.Length);
// create a bitmap: 100x100 pixels, 2bytes/pixel, 16bitgrayscale
Bitmap newBitmap = new Bitmap(100, 100, 2 * 100, PixelFormat.Format16bppGrayScale, hglobal);
// display bitmap
pictureBox1.Image = newBitmap;
// free the memory
Marshal.FreeHGlobal(hglobal);
Run Code Online (Sandbox Code Playgroud) 我有一个以下格式的字符串:
word1.word2.word3
Run Code Online (Sandbox Code Playgroud)
在perl中从该字符串中提取word2的方法有哪些?我尝试了以下表达式,但它将 1 分配给sub:
@perleval $vars{sub} = $vars{string} =~ /.(.*)./; 0@
Run Code Online (Sandbox Code Playgroud)
编辑:
我尝试了几种建议,但仍然得到1的值。我怀疑上面的整个表达式除了解析之外还有问题。但是,当我进行简单的分配时,我得到了正确的结果:
@perleval $vars{sub} = $vars{string} ; 0@
Run Code Online (Sandbox Code Playgroud)
将word1.word2.word3分配给变量sub
如何正确终止在单独线程中启动的 Flask Web 应用程序?我发现了一个不完整的答案,不清楚如何去做。下面的脚本启动一个线程,该线程又启动一个 Flask 应用程序。当我按CTRL+ 时C,某些内容不会被终止,并且脚本永远不会停止。在正确except KeyboardInterrupt:
终止app
和之后添加代码会很好thread_webAPP()
。我知道如何终止线程,但首先我需要终止应用程序:
def thread_webAPP():
app = Flask(__name__)
@app.route("/")
def nothing():
return "Hello World!"
app.run(debug=True, use_reloader=False)
# hope that after app.run() is terminated, it returns here, so this thread could exit
t_webApp = threading.Thread(name='Web App', target=thread_webAPP)
t_webApp.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print("exiting")
# Here I need to kill the app.run() along with the thread_webAPP
Run Code Online (Sandbox Code Playgroud) 我试图将此C++代码转换为C#:
do{
if (Node->NextNode == NULL) WaitForSingleObject(pThis->_Event, INFINITE);
_critSect.Lock();
if (Node->NextNode == NULL && !bRunning )
{
_critSect.Unlock(); // can explicitly unlock here
break;
}
_critSect.Unlock();
}while (Node->NextNode == NULL);
Run Code Online (Sandbox Code Playgroud)
在C++中,我可以显式解锁线程,但C#只有一个括号.如何在破解之前在下面的代码中添加"解锁"功能?
do{
if (Node->NextNode == null) DataQueueEvent.WaitOne();
lock (thisLock)
{
if (Node->NextNode == null && !bRunning)
// need to unlock here!!!
break;
}
} while (Node->NextNode == null);
Run Code Online (Sandbox Code Playgroud) 我正在使用 Tpage(模板工具包的命令行包装器),文档描述了如何为标量、列表或散列变量创建自定义虚拟方法,然后将其称为variable.custom_method()
. 但是,我想在模板中定义一些可以直接使用的独立函数。想法如下:
这是我的functions.tt 文件:
[%- PERL -%]
sub int2hex{
my $intvar = shift;
return sprintf("%X", $intvar);
};
[%- END -%]
[% myInteger=18 %]
Run Code Online (Sandbox Code Playgroud)
这是我的 template.tt 文件:
Some text
The value of [%myInteger%] in hexadecimal is [% int2hex(myInteger) %]
More text
Run Code Online (Sandbox Code Playgroud)
然后,我将从命令行调用模板工具包,如下所示:
tpage --eval_perl --pre_process=functions.tt template.tt > result.f
Run Code Online (Sandbox Code Playgroud)
预期function.tt
文件将首先被处理,并且int2hex()
子例程可用于该template.tt
文件。但是,我收到一条错误消息,指出undef error - int2hex is undefined
. 另一方面,它并不抱怨myInteger
.
看起来[% PERL %][% END %]
该文件的部分没有将该int2hex
函数添加到方法列表中。使用此处描述的宏或插件来实现这一点并不简单 …