WPF .NET Core 应用程序 - 将位图加载到 ui 图像而不先保存它 - C#
WPF .NET Core App - Load bitmap to ui Image without saving it first - C#
我正在尝试切换到新的 WPF 应用程序样式,但到目前为止我非常不以为然。
有没有办法将应用程序生成的位图加载到 PictureBox 中而不先保存?到目前为止,我找到了以下解决方案(我想改进):
UI:
Xaml代码:
<Image x:Name="CurrentFrame_image" HorizontalAlignment="Left" Height="110" Margin="10,10,0,0" VerticalAlignment="Top" Width="190" Grid.ColumnSpan="2"/>
UI-更新码:
public void UpdateProgressFrame(Bitmap currentScreen)
{
currentScreen.Save(@".\progressframe.png");
BitmapImage image = new BitmapImage(new Uri("/progressframe.png", UriKind.Relative));
CurrentFrame_image.Source = image;
}
但是,我非常不高兴每隔几毫秒就将图像保存到磁盘以便在我的应用程序中显示它。有什么直接、快捷的方法吗?
旧的,Winform 风格
public void UpdateProgressFrame(Bitmap currentScreen)
{
CurrentFrame_pictureBox.Image = currentScreen;
}
您可以想象,视频转换磁盘上的 IO 操作对于硬盘性能而言并不是真正的最佳选择,尤其是在较旧的旋转硬盘上。
如果您不想加载,则表示您要在运行时创建位图。
如果是这样,您可以使用此代码:
PictureBox pictureBox1 = new PictureBox();
public void CreateBitmapAtRuntime()
{
pictureBox1.Size = new Size(210, 110);
this.Controls.Add(pictureBox1);
Bitmap flag = new Bitmap(200, 100);
Graphics flagGraphics = Graphics.FromImage(flag);
int red = 0;
int white = 11;
while (white <= 100) {
flagGraphics.FillRectangle(Brushes.Red, 0, red, 200,10);
flagGraphics.FillRectangle(Brushes.White, 0, white, 200, 10);
red += 20;
white += 20;
}
pictureBox1.Image = flag;
}
你可以用你想要的任何东西填充位图,例如你可以用你保存在数据库中的位值创建位图
解决方法:
“保存”位图到内存流和加载流
BitmapImage BitmapToImageSource(ref Bitmap input)
{
BitmapImage bitmapimage = new BitmapImage();
using (MemoryStream memory = new MemoryStream())
{
input.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);
memory.Position = 0;
bitmapimage.BeginInit();
bitmapimage.StreamSource = memory;
bitmapimage.CacheOption = BitmapCacheOption.OnLoad;
bitmapimage.EndInit();
}
return bitmapimage;
}
我正在尝试切换到新的 WPF 应用程序样式,但到目前为止我非常不以为然。
有没有办法将应用程序生成的位图加载到 PictureBox 中而不先保存?到目前为止,我找到了以下解决方案(我想改进):
UI:
Xaml代码:
<Image x:Name="CurrentFrame_image" HorizontalAlignment="Left" Height="110" Margin="10,10,0,0" VerticalAlignment="Top" Width="190" Grid.ColumnSpan="2"/>
UI-更新码:
public void UpdateProgressFrame(Bitmap currentScreen)
{
currentScreen.Save(@".\progressframe.png");
BitmapImage image = new BitmapImage(new Uri("/progressframe.png", UriKind.Relative));
CurrentFrame_image.Source = image;
}
但是,我非常不高兴每隔几毫秒就将图像保存到磁盘以便在我的应用程序中显示它。有什么直接、快捷的方法吗?
旧的,Winform 风格
public void UpdateProgressFrame(Bitmap currentScreen)
{
CurrentFrame_pictureBox.Image = currentScreen;
}
您可以想象,视频转换磁盘上的 IO 操作对于硬盘性能而言并不是真正的最佳选择,尤其是在较旧的旋转硬盘上。
如果您不想加载,则表示您要在运行时创建位图。 如果是这样,您可以使用此代码:
PictureBox pictureBox1 = new PictureBox();
public void CreateBitmapAtRuntime()
{
pictureBox1.Size = new Size(210, 110);
this.Controls.Add(pictureBox1);
Bitmap flag = new Bitmap(200, 100);
Graphics flagGraphics = Graphics.FromImage(flag);
int red = 0;
int white = 11;
while (white <= 100) {
flagGraphics.FillRectangle(Brushes.Red, 0, red, 200,10);
flagGraphics.FillRectangle(Brushes.White, 0, white, 200, 10);
red += 20;
white += 20;
}
pictureBox1.Image = flag;
}
你可以用你想要的任何东西填充位图,例如你可以用你保存在数据库中的位值创建位图
解决方法: “保存”位图到内存流和加载流
BitmapImage BitmapToImageSource(ref Bitmap input)
{
BitmapImage bitmapimage = new BitmapImage();
using (MemoryStream memory = new MemoryStream())
{
input.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);
memory.Position = 0;
bitmapimage.BeginInit();
bitmapimage.StreamSource = memory;
bitmapimage.CacheOption = BitmapCacheOption.OnLoad;
bitmapimage.EndInit();
}
return bitmapimage;
}