<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: releasing resources with IExportBmp in ArcObjects SDK Questions</title>
    <link>https://community.esri.com/t5/arcobjects-sdk-questions/releasing-resources-with-iexportbmp/m-p/698492#M18715</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;OK after several days of painful debugging I've fixed my problem&lt;/P&gt;&lt;P&gt;The problem is not with the Image.FromHBitmap() method it is in the ExportBmpClass.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;Int32 hDC = exportBmpClass.StartExporting();
activeView.Output(hDC, (int)exportBmpClass.Resolution, ref exRect, null, null);

// save the handle
int hBmp1 = exportBmpClass.Bitmap;

exportBmpClass.finishExporting();
// save the handle again
int hBmp2 = exportBmpClass.Bitmap;
// output handle values to compare.&amp;nbsp; they are different!
Console.WriteLine("hBmp1 = " + hBmp +", hBmp2 = " + hBmp2); 

Bitmap bmp = Image.FromHbitmap(bmp1);
// the other handle also would work
&lt;SPAN style="color: rgba(0, 0, 0, 0); font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px;"&gt;//Bitmap bmp = Image.FromHbitmap(bmp2);&lt;/SPAN&gt;

&lt;SPAN style="color: rgba(0, 0, 0, 0); font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px;"&gt;exportBmpClass.Cleanup();]&lt;/SPAN&gt;

&lt;SPAN style="color: rgba(0, 0, 0, 0); font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px;"&gt;//now both HBitmaps must be deleted&lt;/SPAN&gt;
&lt;SPAN style="color: rgba(0, 0, 0, 0); font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px;"&gt;DeleteObject(hBmp1);&lt;/SPAN&gt;
&lt;SPAN style="color: rgba(0, 0, 0, 0); font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px;"&gt;DeleteObject(hBmp2);&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For some reason the ExportBmpClass is making a copy at the finishExporting() call and looses its first handle.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 12 Dec 2021 05:24:08 GMT</pubDate>
    <dc:creator>IainGillies</dc:creator>
    <dc:date>2021-12-12T05:24:08Z</dc:date>
    <item>
      <title>releasing resources with IExportBmp</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/releasing-resources-with-iexportbmp/m-p/698491#M18714</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;I am writing a program to render a map as a in-memory Bitmap that then sends the data to another application.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I've followed the ExportJpg example here.&lt;/P&gt;&lt;P&gt;&lt;A href="http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#/Create_JPEG_from_ActiveView_Snippet/004900000064000000/" title="http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#/Create_JPEG_from_ActiveView_Snippet/004900000064000000/" rel="nofollow noopener noreferrer" target="_blank"&gt;ArcObjects 10 .NET SDK Help&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The problem is the exporter creates a GDI HBitmap.&amp;nbsp; I want a .Net Bitmap so I'm using Systems.Drawing.Image.FromHbitmap()&lt;/P&gt;&lt;P&gt;Microsoft sdk &lt;A href="https://msdn.microsoft.com/en-us/library/k061we7x(v=vs.90).aspx" title="https://msdn.microsoft.com/en-us/library/k061we7x(v=vs.90).aspx" rel="nofollow noopener noreferrer" target="_blank"&gt;Image.FromHbitmap Method (IntPtr) (System.Drawing)&lt;/A&gt; says that you have to use the gdi32.dll call to DeleteObject() to release the GDI Object manually after using FromHbitmap().&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;What I am finding is that the GDI handle that is created by FromHBitmap is never released and the program leaks a GDI Object every time the new bmp is exported.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;// activeView and exportBmpClass are already setup similar to ExportJPJ example

Int32 hDC = exportBmpClass.StartExporting(); // Creates 1 DC and 1 HBitmap
activeView.Output(hDC, (int)exportBmpClass.Resolution, ref exRect, null, null); //renders activeview to the hDC/ HBitmap

Bitmap bmp = Image.FromHbitmap((IntPtr)exportBmpClass.Bitmap, (IntPtr)exportBmpClass.Palette); // creates 1 HBitmap

DeleteObject((IntPtr)exportBmpClass.Bitmap); // gdi32.dll call does not release a HBitmap!

exportBmpClass.FinishExporting(); // releases 1 DC
exportBmpClass.Cleanup(); //releases 1 HBitmap

// rest of code, do stuff with the .Net bmp ...&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Any ideas?&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;Iain&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 05:24:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/releasing-resources-with-iexportbmp/m-p/698491#M18714</guid>
      <dc:creator>IainGillies</dc:creator>
      <dc:date>2021-12-12T05:24:05Z</dc:date>
    </item>
    <item>
      <title>Re: releasing resources with IExportBmp</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/releasing-resources-with-iexportbmp/m-p/698492#M18715</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;OK after several days of painful debugging I've fixed my problem&lt;/P&gt;&lt;P&gt;The problem is not with the Image.FromHBitmap() method it is in the ExportBmpClass.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;Int32 hDC = exportBmpClass.StartExporting();
activeView.Output(hDC, (int)exportBmpClass.Resolution, ref exRect, null, null);

// save the handle
int hBmp1 = exportBmpClass.Bitmap;

exportBmpClass.finishExporting();
// save the handle again
int hBmp2 = exportBmpClass.Bitmap;
// output handle values to compare.&amp;nbsp; they are different!
Console.WriteLine("hBmp1 = " + hBmp +", hBmp2 = " + hBmp2); 

Bitmap bmp = Image.FromHbitmap(bmp1);
// the other handle also would work
&lt;SPAN style="color: rgba(0, 0, 0, 0); font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px;"&gt;//Bitmap bmp = Image.FromHbitmap(bmp2);&lt;/SPAN&gt;

&lt;SPAN style="color: rgba(0, 0, 0, 0); font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px;"&gt;exportBmpClass.Cleanup();]&lt;/SPAN&gt;

&lt;SPAN style="color: rgba(0, 0, 0, 0); font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px;"&gt;//now both HBitmaps must be deleted&lt;/SPAN&gt;
&lt;SPAN style="color: rgba(0, 0, 0, 0); font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px;"&gt;DeleteObject(hBmp1);&lt;/SPAN&gt;
&lt;SPAN style="color: rgba(0, 0, 0, 0); font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px;"&gt;DeleteObject(hBmp2);&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For some reason the ExportBmpClass is making a copy at the finishExporting() call and looses its first handle.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 05:24:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/releasing-resources-with-iexportbmp/m-p/698492#M18715</guid>
      <dc:creator>IainGillies</dc:creator>
      <dc:date>2021-12-12T05:24:08Z</dc:date>
    </item>
  </channel>
</rss>

