none
Process "EMR_STRETCHDIBITS" record type in EMF RRS feed

  • Question

  • Hi,

    I have a requirement to create an image (which may be color and save as black and white png/bitmap) from EMF "EMR_STRETCHDIBITS" record type. I'm able to read all the data for the corresponding record type including the image byte information. It would be very helpful if you can provide some guidelines/sample code (in c#) to create an image from 'EMR_STRETCHDIBITS'.

    Also in my EMF, only one image is present, but when we parse the EMF, 'EMR_STRETCHDIBITS' is appearing twice. Only difference is in the TernaryRatserOperation enum value (we are getting one with 'SRCPAINT' and other with 'SRCAND' ). I googled and found it that these combination is using to create transparent image. How can I combine these two to create my desired output?

    Thanks in advance

    Tuesday, September 24, 2013 6:01 AM

Answers

  • Hi Renjith,

    I believe that few people here got experience in "EMR_STRETCHDIBITS" record processing. So could you please share some code you've already got?

    Seems that somebody else have resolve this problem in another language. Please see:

    http://stackoverflow.com/questions/10145340/extract-images-from-windows-emf-during-enumenhmetafile-parse

    I think you could try to convert the code snippet to C#.

    The following code is what I converted from the Vincent Povirk's code in the above link using C++ to C# Converter:

    using System;
    
    private System.IntPtr bitmap_from_stretchdibits(System.IntPtr hdc, ENHMETARECORD lpEMFR)
    {
    	EMRSTRETCHDIBITS pStretchDIBits = (EMRSTRETCHDIBITS)lpEMFR;
    	BITMAP bm = new BITMAP();
    	System.IntPtr hbm;
    
    	hbm = CreateDIBitmap(hdc, (BITMAPINFO)((byte)lpEMFR + pStretchDIBits.offBmiSrc), pStretchDIBits.iUsageSrc, null, null, 0);
    
    	if (hbm)
    	{
    		GetObjectA(hbm, sizeof(BITMAP), bm);
    
    		SetDIBits(hdc, hbm, 1, Math.Abs(bm.bmHeight), (byte)lpEMFR + pStretchDIBits.offBitsSrc, (BITMAPINFO)((byte)lpEMFR + pStretchDIBits.offBmiSrc), pStretchDIBits.iUsageSrc);
    	}
    
    	return hbm;
    }

    About EMRSTRETCHDIBITS structure, please see the definition in C++:

    http://msdn.microsoft.com/en-us/library/windows/desktop/dd162590(v=vs.85).aspx

    Please check if it helps.


    Caillen
    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, September 25, 2013 6:36 AM
    Moderator
  • To create an image from the Metafile, use DrawImage to play it back to a Bitmap.  Use an ImageAttributes overload and set the ColorMatrix to draw a black and white image.  Save the Bitmap using the ImageFormat you desire.

    Wednesday, September 25, 2013 9:32 AM

All replies

  • Hi Renjith,

    I believe that few people here got experience in "EMR_STRETCHDIBITS" record processing. So could you please share some code you've already got?

    Seems that somebody else have resolve this problem in another language. Please see:

    http://stackoverflow.com/questions/10145340/extract-images-from-windows-emf-during-enumenhmetafile-parse

    I think you could try to convert the code snippet to C#.

    The following code is what I converted from the Vincent Povirk's code in the above link using C++ to C# Converter:

    using System;
    
    private System.IntPtr bitmap_from_stretchdibits(System.IntPtr hdc, ENHMETARECORD lpEMFR)
    {
    	EMRSTRETCHDIBITS pStretchDIBits = (EMRSTRETCHDIBITS)lpEMFR;
    	BITMAP bm = new BITMAP();
    	System.IntPtr hbm;
    
    	hbm = CreateDIBitmap(hdc, (BITMAPINFO)((byte)lpEMFR + pStretchDIBits.offBmiSrc), pStretchDIBits.iUsageSrc, null, null, 0);
    
    	if (hbm)
    	{
    		GetObjectA(hbm, sizeof(BITMAP), bm);
    
    		SetDIBits(hdc, hbm, 1, Math.Abs(bm.bmHeight), (byte)lpEMFR + pStretchDIBits.offBitsSrc, (BITMAPINFO)((byte)lpEMFR + pStretchDIBits.offBmiSrc), pStretchDIBits.iUsageSrc);
    	}
    
    	return hbm;
    }

    About EMRSTRETCHDIBITS structure, please see the definition in C++:

    http://msdn.microsoft.com/en-us/library/windows/desktop/dd162590(v=vs.85).aspx

    Please check if it helps.


    Caillen
    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, September 25, 2013 6:36 AM
    Moderator
  • To create an image from the Metafile, use DrawImage to play it back to a Bitmap.  Use an ImageAttributes overload and set the ColorMatrix to draw a black and white image.  Save the Bitmap using the ImageFormat you desire.

    Wednesday, September 25, 2013 9:32 AM