开发者

Custom form shape slow to load, store Region on disk! c#

开发者 https://www.devze.com 2023-01-20 10:01 出处:网络
hi I\'m using code from the following link and using开发者_开发技巧 the fast / unsafe option. however on slower pc\'s there is still a lag.

hi I'm using code from the following link and using开发者_开发技巧 the fast / unsafe option. however on slower pc's there is still a lag. http://www.vcskicks.com/custom_shape_form_region.php

I was wondering if on first run the app creates the region and then stores it somewhere or even I supply it with the app if that would speed it up!??

if so how would you save and read a region?

or is there a better/faster approach I could use?

it's a .net4 windows app.

cheers


Converting a bitmap to a Region is a fundamentally slow process. However, I saw the author mentioning GetPixel() which is about as slow you could ever make it. Reading pixels in a bitmap can be a lot faster if you use an unsafe pointer.

The code in this web page does so and generates the Region you need.


The region object can't be directly serialized as it isn't marked with Serializable. However, you can easily serialize it anyways and I agree that reading the region from a file will lead to increased speed on slower machines.

The method shown below will probably be enough for you and shows the idea but it would make more sense in production code to save region.dat as a resource and compile it with your application.

  1. Move the GraphicsPath to the argument list in getRegionFast (we need to get it back), so it looks like this:

    public unsafe static Region getRegionFast(Bitmap bitmap, Color transparencyKey, int tolerance, GraphicsPath path)

  2. Remove path.Dispose() from that method.

  3. Create this class:

    [Serializable]
    public class SerializedData
    {
       public SerializedData(GraphicsPath path) 
       {
        Points = new List<PointF>(path.PathPoints);
        Types = new List<byte>(path.PathTypes);
       }            
       public List<PointF> Points { get; set; }
       public List<byte> Types { get; set; }
    

    }

  4. Change the Form1 constructor so the region is set like this:

    this.Region = GetRegionTryCached();
    
  5. Copy/paste the GetRegionTryCached method into Form1:

    private Region GetRegionTryCached()
    {
        const string filePath = @".\path.dat";           
    
    
    
    // Check if the path has been serialized:
    if (File.Exists(filePath))
    {                
    using (FileStream fs = File.OpenRead(filePath))
    {
        BinaryFormatter formatter = new BinaryFormatter();
        SerializedData data = (SerializedData)formatter.Deserialize(fs);
        GraphicsPath path = new GraphicsPath(data.Points.ToArray(), data.Types.ToArray());
        return new Region(path);
    }
    }
    else
    {
    GraphicsPath path = new GraphicsPath();
    
    
    // Create it as before:                
    Region region = BitmapToRegion.getRegionFast((Bitmap)this.BackgroundImage, Color.FromArgb(0, 255, 0), 100, path);                                               
    
    
    BinaryFormatter formatter = new BinaryFormatter();
    // Serialize the path:
    using (FileStream fs = File.Open(filePath, FileMode.Create, FileAccess.Write))
    {
        SerializedData data = new SerializedData(path);
        formatter.Serialize(fs, data);
        path.Dispose();
    }
    
    
    return region;
    }            
    
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号