使用 N x N 图像网格生成 WPF 图像

发布于 2024-10-03 01:55:50 字数 1530 浏览 0 评论 0原文

我正在开发一个个人项目,该项目从图像网格创建单个图像。生成图像需要一段时间,并且只有代码执行完毕后才每次刷新。如何使界面在生成图像时仍然有效(不锁定)。

那么开始吧: 我有一个 N x N 标识符网格,根据标识符,我在 (x,y) 处绘制具有给定缩放高度和宽度的特定图像。

该图像每次迭代都会重新生成,并且需要在 WPF 上更新。它还绑定到 xaml 端图像的 ImageSource

我的问题是“如何提高生成此大图像的性能”和“如何根据需要多次刷新图像(每代)”。

for (int i = 0; i < numberOfIterations; i++)
{
  // Do Some Work
  UpdateImage();
}
...
BitmapImage imgFlower = new BitmapImage(new Uri(@"Images\Flower.bmp", UriKind.Relative));
BitmapImage imgPuppy = new BitmapImage(new Uri(@"Images\Puppy.bmp", UriKind.Relative));
ImageSource GeneratedImage{ get{ GenerateImage(); } set; } 
...
void UpdateImage() { OnPropertyChanged("GeneratedImage"); }
...
ImageSource GenerateImage()
{
  RenderTargetBitmap bmp = new RenderTargetBitmap(223, 223, 96, 96, PixelFormats.Pbgra32);
  DrawingVisual drawingVisual = new DrawingVisual();
  using (DrawingContext drawingContext = drawingVisual.RenderOpen())
  {
    double scaleRatio = CalculateScaleRatio();
    DrawGridOfImages(drawingContext, scaleRatio);
  }
  bmp.Render(drawingVisual);
  return bmp;
}
...
DrawGridOfImages(...)
{
  double x,y;
  for (int r = 0; r < NumberOfRows; r++)
  {
    x = r * scaleRatio;
    for (int c = 0; c < NumberOfColumns; c++)
    {
      y = c * scaleRatio;
      switch (imageOccupancy[r, c])
      {
         case Flower: drawingContext.DrawImage(imgFlower, new Rect(x,y,scaleRatio,scaleRation));
         case Puppy: drawingContext.DrawImage(imgPuppy, new Rect(x,y,scaleRatio,scaleRatio));
      }
    }
  }
}

I'm working on a personal project that creates an single image from a grid of images. It takes a while to generate the image and doesn't refresh everytime only once the code is done executing. How can the make the interface still functional (not locked up) when its generating the image.

So to start:
I have a N x N grid of identifiers, based on the identifier I draw a specific image at (x,y) with a given scaled height and width.

This image is regenerated each iteration and needs to be updated on the WPF. It is also bound to the ImageSource of the Image on the xaml side

My issue is 'How do I improve performance of generating this large image' and 'How do I refresh the image as many times as I need to (per generation).

for (int i = 0; i < numberOfIterations; i++)
{
  // Do Some Work
  UpdateImage();
}
...
BitmapImage imgFlower = new BitmapImage(new Uri(@"Images\Flower.bmp", UriKind.Relative));
BitmapImage imgPuppy = new BitmapImage(new Uri(@"Images\Puppy.bmp", UriKind.Relative));
ImageSource GeneratedImage{ get{ GenerateImage(); } set; } 
...
void UpdateImage() { OnPropertyChanged("GeneratedImage"); }
...
ImageSource GenerateImage()
{
  RenderTargetBitmap bmp = new RenderTargetBitmap(223, 223, 96, 96, PixelFormats.Pbgra32);
  DrawingVisual drawingVisual = new DrawingVisual();
  using (DrawingContext drawingContext = drawingVisual.RenderOpen())
  {
    double scaleRatio = CalculateScaleRatio();
    DrawGridOfImages(drawingContext, scaleRatio);
  }
  bmp.Render(drawingVisual);
  return bmp;
}
...
DrawGridOfImages(...)
{
  double x,y;
  for (int r = 0; r < NumberOfRows; r++)
  {
    x = r * scaleRatio;
    for (int c = 0; c < NumberOfColumns; c++)
    {
      y = c * scaleRatio;
      switch (imageOccupancy[r, c])
      {
         case Flower: drawingContext.DrawImage(imgFlower, new Rect(x,y,scaleRatio,scaleRation));
         case Puppy: drawingContext.DrawImage(imgPuppy, new Rect(x,y,scaleRatio,scaleRatio));
      }
    }
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我入戏太深 2024-10-10 01:55:50

有两种方法。首先也是最有益的是提高感知性能,方法是在工作线程上生成图像并使用事件在关键点更新 UI 线程上的图像,以便用户可以看到进度。

为了提高实际性能,如果您的目标是并使用多核系统,则您可以尝试并行函数(如果您的迭代实际上可以并行执行)。这将需要一些工作和不同的思维方式,但如果您付出努力,将会有所帮助。我建议您学习 PLINQ 以开始使用。

There are two ways. To first and most beneficial would be to improve the perceived performance, do this by generating the image on a worker thread and use events to update the image on the UI thread at key points so your users can see the progress.

To improve actual performance, if you are targeting and using multicore systems you can try parallel functions if your iterations can actually be performed in parallel. This will require some work and a different mindset but will help if you put the effort in. I'd recommend studying PLINQ to get started.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文