Right way to dispose Image/Bitmap and PictureBox

前端 未结 3 1973
慢半拍i
慢半拍i 2020-11-27 18:14

I am trying to develop a Windows Mobile 6 (in WF/C#) application. There is only one form and on the form there is only a PictureBox object. On it I draw all desired controls

3条回答
  •  萌比男神i
    2020-11-27 18:47

    1: I dont know if it works in Windows Mobile but try this:

    FileStream bitmapFile = new FileStream("mybitmap.bmp", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    Image loaded = new Bitmap(bitmapFile);
    

    2: The SolidBrush must be disposed. There is a general rule for dispose. --> "every object, instanciated by you, that implements dispose must be disposed manually, exept when the object is a return/ref/out value"

    In this case it is better to use a using statement

    using (new objecttodispose){ ..... } 
    

    The using statement will ensure the call of Dispose() in any case (exception for example).

    3: Dispose() will free the bitmap ressources.

提交回复
热议问题