[WPF] Canvas convert a lot of problems for the local picture

Original Address: https://www.cnblogs.com/younShieh/p/11279420.html

  Project encountered a problem, we need to not show up hundreds of Canvas stored as images stored locally.
1. access to information after (Baidu,) that can be saved as a local picture BitmapSource conversion by PngBitmapEncoder () to achieve. Specific code as follows:

//path为保存路径
using (FileStream outStream = new FileStream(path, FileMode.Create))
{
    PngBitmapEncoder encoder = new PngBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
    encoder.Save(outStream);
}

2. How to convert a bitmap image into a way of knowing, the next step is to take Canvas converted into a Bitmap. RenderTargetBitmap () method to convert Visual bitmap objects can be achieved. View Canvas Canvas is inherited, so in theory it should be no problem. Try a little, indeed. code show as below:

 RenderTargetBitmap bitmap = new RenderTargetBitmap((int)canvas.ActualWidth, (int)gd.ActualHeight, 96, 96, PixelFormats.Pbgra32);
 bitmap.Render(canvas);

3. In theory it is done here, but my Canvas does not show up, leading to two ActualHeight ActualWidth and property values ​​are 0, and can not be changed by setting the size of the actual size, which is a little pull the skull. It would continue to check the information bar. For ActualWidth and ActualHeight these two properties, CSDN says:

This property is calculated based on the value and the other width of the input layout of the system.
Rendering value based on actual transmission is provided by the layout system itself, and therefore may be slightly smaller than the set value of the property as a basis for such an input Width changes.

Because ActualWidth is a calculated value, it should be noted there may be multiple or incremental reported changes to it as a result of various operations by the layout system. Layout system may calculate the desired sub-element of a measurement space, and other constraints of the parent element.

Although you can not set this property from XAML, you can style based on the value of Trigger.

  That ActualWidth and ActualHeight is not set, it is automatically calculated by the actual presentation. But I have not presented the Canvas, ah, we can not be passive calculated, and this can be how to do it. . . Not passive calculation, only take the initiative to set up, take the initiative to calculate this road can go. . . But also access to information, or have found a way to go.

https://stackoverflow.com/questions/5189139/how-to-render-a-wpf-usercontrol-to-a-bitmap-without-creating-a-window

  Moguls learned by the learning experience, the interface is not shown can be converted into a bitmap, the primary need to measure the Measure () and positioning the Arrange () to set the location and sizes of the Canvas, may be formed by two methods recursive layout update. That these two methods, provided the final size of the parent element as a child of the calculation, that is, the actual size.

canvas.Measure(new Size(300, 300));
canvas.Arrange(new Rect(new Size(300,300)));

4. In theory it is done here, but my Cancas are also a prerequisite is that they have a lot. (The problem is a bit much ...) too many Canvas I need to save, so live up to expectations, my memory burst (to burst friends ~). But I've done automatic recovery, no reason ah. Problem I am trying to find a lot of places that may occur, generous use GC.Collect (); Dispose (); using () and so on and so on, but do not do the eggs with. . . Buckle broken skull, shield section of the code section of the code, the distribution of the investigation in the end is where the problem is. I thought it was a memory leak caused by the conversion map, but after step debugging discovered that in fact is not how memory consumption, and have done a reasonable recovery. After commissioning slowly I found out that my measurements Measure () and positioning Arrange () These two methods take up a lot of memory, and should not be recovered.

5. First of all I would like is that I can not do these two methods. Because I can not show the interface, so I want him to go inside the rendering into memory, if not render it, directly RenderTargetBitmap () dimensions get is a blank chart (tried ..). After check the information for a long time, indeed not found the right way, can do without the picture does not show the Arrange () method can be converted to bitmaps. So I have to use these two methods, so it can not opportunistic, and can only bite the bullet on.

6. Baidu, google turns on stage, MSDN, stackoverflow brightest fly ,,, did not find a suitable solution. Perhaps I did not encounter such a special case of folks, there may be a problem with the way my search. However, I was broken skull buckle occasion, I think Canvas will also have a corresponding Dispose () methods? (Forgive me have fainted, "Dispose () can only be used on IDisposable class inheritance" of knowledge already flown to the winds), however, Canvas does not have a Dispose () method,, sad, desperate. It would not have Arrange () corresponding Dispose () methods? After attempts to enter Arrage Canvas, to give a InvalidateArrange () Method:

State that the arrangement of elements (layout) is invalid. After alignment state fails, the element will update its layout, the update will occur asynchronously, unless followed by a forced System.Windows.UIElement.UpdateLayout performed.

  The element arrangement state (layout) is invalid, not that release the memory occupied by the distribution of resources? Try a little, really the case. Memory consumption is no longer a "line of egrets on the sky," the. . . Alas, burst memory of this problem is resolved, my mother no longer have to worry about software crashes my friends ~ ~ ~

With code as follows:

canvas.Measure(new System.Windows.Size(1920, 1080));
canvas.Arrange(new Rect(0, 0, 1920, 1080));
renderBitmap.Render(canvas);
canvas.InvalidateArrange();
canvas.InvalidateMeasure();
canvas.UpdateLayout();

The resulting lesson is that the code still have to knock yourself. . . Otherwise, you fall into the pit do not know how to climb out. . .

Kick call it a day.

Guess you like

Origin www.cnblogs.com/younShieh/p/11279420.html