Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / .NET SDK / November 2005

Tip: Looking for answers? Try searching our database.

How do I save a bitmap as a metafile?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
David Thielen - 08 Nov 2005 20:42 GMT
Hi;

I tried to take an Image that is a bitmap and save it as a metafile (ie one
record, a DIB) using: img.Save(mem,
System.Drawing.Imaging.ImageFormat.get_Wmf());

However, I get the following:
java.lang.IllegalArgumentException: Value cannot be null.
Parameter name: encoder
  at System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder,
EncoderParameters encoderParams)
  at System.Drawing.Image.Save(Stream stream, ImageFormat format)
  at TestImage.Program.main(String[] args) in
C:\src\wrDotNet\TestImage\Program.jsl:line 32
  at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
  at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
  at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
  at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
  at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
  at System.Threading.ThreadHelper.ThreadStart()

It obviously wants EncoderParameters but I can find no documentation as to
what to set in this for a wmf file.

What do I need to do?

Signature

thanks - dave

"Peter Huang" [MSFT] - 09 Nov 2005 06:48 GMT
Hi

Based on my research, we can only readonly about wmf.

316563 PRB: Image.Save Method Does Not Save the File as the Selected File
Type
http://support.microsoft.com/?id=316563

You may try to check it with code below.
            ImageCodecInfo[] icis= ImageCodecInfo.GetImageEncoders();
            for(int i =0;i<icis.Length;i++)
                Debug.WriteLine(icis[i].CodecName);
            icis= ImageCodecInfo.GetImageDecoders();
            for(int i =0;i<icis.Length;i++)
                Debug.WriteLine(icis[i].CodecName);

So I think you just need to save it as BMP is OK.

Best regards,

Peter Huang
Microsoft Online Partner Support

Signature

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

David Thielen - 09 Nov 2005 12:32 GMT
Hi;

They should put this in the documentation for Save(). Would have saved me
some time.

There is a Metafile class - but it's constructors appear to require a device
context and it appears to only create enhanced metafiles. Any suggestions on
the best way to create a placeable wmf (not emf)?

Signature

thanks - dave

> Hi
>
[quoted text clipped - 21 lines]
> Get Secure! - www.microsoft.com/security
> This posting is provided "AS IS" with no warranties, and confers no rights.
"Peter Huang" [MSFT] - 10 Nov 2005 08:18 GMT
Hi

Based on my research, there is function in GDI will do the job to convert
EMF to WMF. The GetWinMetaFileBits function converts the enhanced-format
records from a metafile into Windows-format records and stores the
converted records in the specified buffer. Also in the GDI+  there is
EmfToWmfBits method to do the convert.

Here is a link you may have a look.
Insert Image into rtf document
http://dotnet.org.za/pieter/archive/category/873.aspx

Best regards,

Peter Huang
Microsoft Online Partner Support

Signature

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

David Thielen - 10 Nov 2005 14:29 GMT
Hello;

I'm sorry I think you misunderstood me. I have a bitmap (bmp format) and I
need to create a wmf that is composed of just that bitmap. So basically just
a STRETCHDIBITS record. How can I do that in .net?

Signature

thanks - dave

> Hi
>
[quoted text clipped - 15 lines]
> Get Secure! - www.microsoft.com/security
> This posting is provided "AS IS" with no warranties, and confers no rights.
"Peter Huang" [MSFT] - 11 Nov 2005 08:13 GMT
Hi

We can draw on a EMF file, but and use the approach I refer to in the last
link to convert it into WMF.
        private void button4_Click(object sender, System.EventArgs e)
        {
            Graphics g= this.CreateGraphics();
            IntPtr hDC = g.GetHdc();
            Metafile mf = new Metafile(hDC,EmfType.EmfOnly);
            g.ReleaseHdc(hDC);
            g.Dispose();
            g=Graphics.FromImage(mf);
            g.DrawImage(Bitmap.FromFile(@"c:\temp\test.bmp"),0,0);
            g.Dispose();
            mf.Save(@"C:\temp\Dest.emf");

        }

You may have a try.

Best regards,

Peter Huang
Microsoft Online Partner Support

Signature

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

"Peter Huang" [MSFT] - 11 Nov 2005 08:35 GMT
Hi,

Currently I am researching the issue and we will reply here with more
information as soon as possible.
If you have any more concerns on it, please feel free to post here.

Thanks for your understanding!

Best regards,

Peter Huang
Microsoft Online Partner Support

Signature

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

"Peter Huang" [MSFT] - 12 Nov 2005 03:06 GMT
Hi

Based on my research, .NET did expose the approach to create an EMF, but we
did not provide the encoder to save it into disk, due to the KB below.
http://support.microsoft.com/?id=316563.

So we need to that in GDI/GDI+ unmanaged code.
        [DllImport("gdiplus.dll")]
        private static extern uint GdipEmfToWmfBits (IntPtr _hEmf, uint
_bufferSize,
            byte[] _buffer, int _mappingMode, EmfToWmfBitsFlags _flags);
        [DllImport("gdi32.dll")]
        private static extern IntPtr SetMetaFileBitsEx (uint _bufferSize,
            byte[] _buffer);
        [DllImport("gdi32.dll")]
        private static extern IntPtr CopyMetaFile (IntPtr hWmf,
            string filename);
        [DllImport("gdi32.dll")]
        private static extern bool DeleteMetaFile (IntPtr hWmf);
        [DllImport("gdi32.dll")]
        private static extern bool DeleteEnhMetaFile (IntPtr hEmf);
        private void button4_Click(object sender, System.EventArgs e)
        {
            Graphics g= this.CreateGraphics();
            IntPtr hDC = g.GetHdc();
            Metafile mf = new Metafile(hDC,EmfType.EmfOnly);
            g.ReleaseHdc(hDC);
            g.Dispose();
            g=Graphics.FromImage(mf);
            //Pen p = new Pen(Color.White,5);
            g.DrawArc(Pens.Black,0,0,200,200,0,360);
            //g.DrawImage(Bitmap.FromFile(@"c:\temp\test.bmp"),0,0);
            g.Dispose();
            IntPtr _hEmf= mf.GetHenhmetafile();
            uint _bufferSize = GdipEmfToWmfBits(_hEmf, 0, null, MM_ANISOTROPIC,
                EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault);
            byte[] _buffer = new byte[_bufferSize];
            GdipEmfToWmfBits(_hEmf, _bufferSize, _buffer, MM_ANISOTROPIC,
                   EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault);
            IntPtr hmf = SetMetaFileBitsEx(_bufferSize, _buffer);
            CopyMetaFile(hmf, "C:\\ConvertedMetafile.wmf");
            DeleteMetaFile(hmf);
            DeleteEnhMetaFile(_hEmf);
        }

If you still have any concern I think you would better post in the gdi
newsgroup.
microsoft.public.win32.programmer.gdi

Best regards,

Peter Huang
Microsoft Online Partner Support

Signature

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

David Thielen - 13 Nov 2005 23:31 GMT
Yuck - so far we are 100% managed code. I think I will port our java code
over to do this to keep us 100%.

Thank you for the example though. I will use this to test that our java code
matches what Windows does

Signature

thanks - dave

> Hi
>
[quoted text clipped - 52 lines]
> Get Secure! - www.microsoft.com/security
> This posting is provided "AS IS" with no warranties, and confers no rights.
"Peter Huang" [MSFT] - 15 Nov 2005 07:05 GMT
Hi

You are welcomed!

Best regards,

Peter Huang
Microsoft Online Partner Support

Signature

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.