Using .NET Components in COM 
Author Message
 Using .NET Components in COM

I kept getting an error from my asp page: "System.Drawing: The type
initializer for "System.Drawing.SafeNativeMethods" threw an exception."
Any one has any idea?

C# code for the component:

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace ImageProj
{
 /// <summary>
 /// Summary description for ImageProj.
 /// </summary>

 [GuidAttribute("****GUID****")]

 public class ImageMagic
 {

  private System.Drawing.Bitmap m_bitmap, image;
  public int OriginalWidth, Width;
  public int OriginalHeight, Height;
  public string Format = "JPG";
  private System.Drawing.Size size;
  private string error = "";

  public ImageMagic()
  {
   m_bitmap = null;
   image = null;
  }

  [DispId(1)]

  public int Save(string strFileName)
  {
   size = new Size(Width, Height);
   image = new Bitmap(m_bitmap, size);

   try
   {
    switch(Format)
    {
     case "BMP":
      image.Save(strFileName, System.Drawing.Imaging.ImageFormat.Bmp);
      break;

     case "JPG":
      image.Save(strFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
      break;

     case "GIF":
      image.Save(strFileName, System.Drawing.Imaging.ImageFormat.Gif);
      break;

     case "TIF":
      image.Save(strFileName, System.Drawing.Imaging.ImageFormat.Tiff);
      break;
    }
   }
   catch (Exception e)
   {
    error = "Error: " + e;
    return 2;
   }
   return -1;

  }

  [DispId(2)]

  public int Open(string strFileName)
  {
   try
   {
    m_bitmap = new Bitmap(strFileName);
    OriginalWidth = m_bitmap.Width;
    OriginalHeight = m_bitmap.Height;
   }
   catch (Exception e)
   {
    error = "Error: " + e;
    return 1;
   }
   return -1;
  }
 }

Quote:
}

My asp code:

<%
Set Jpeg = Server.CreateObject("ImageProj.ImageMagic")

Path = "baboon.jpg"

' Open source image
response.write Jpeg.Open(Path)

response.write Jpeg.OriginalWidth & "<br>"
response.write Jpeg.OriginalHeight & "<br>"
Jpeg.Format = "GIF"
Jpeg.Width = 200
Jpeg.Height = 200

response.write Jpeg.Save("baboon.gif")

set Jpeg = nothing

response.write "done"
%>

I have tested with images in the same folder with a vbs script similar to
the above asp code, and it worked.

Thanks,
Tim



Mon, 29 Nov 2004 14:20:14 GMT  
 Using .NET Components in COM
Hello Tim,

It is because that System.Drawing classes create GDI+ objects that require
the ability to create hidden windows on the desktop to do their
manipulation. However, the web page does not have a desktop to work with,
thus the exception is thrown.

I have found a related question and solution, you may get it from the link
below:

http://dotnet247.com/247reference/msgs/12/63090.aspx

Hope it helps.

Best regards,

Lion Shi, MCSE, MCSD
Microsoft Support Engineer

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use.  2001 Microsoft Corporation. All rights
reserved.
--------------------

    Subject: Using .NET Components in COM
    Date: Thu, 13 Jun 2002 01:20:14 -0500
    Lines: 125
    X-Priority: 3
    X-MSMail-Priority: Normal
    X-Newsreader: Microsoft Outlook Express 6.00.2600.0000
    X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000

    Newsgroups: microsoft.public.dotnet.languages.csharp
    NNTP-Posting-Host: c68.112.113.115.stc.mn.charter.com 68.112.113.115
    Path: cpmsftngxa07!tkmsftngp01!tkmsftngp05
    Xref: cpmsftngxa07 microsoft.public.dotnet.languages.csharp:67818
    X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

    I kept getting an error from my asp page: "System.Drawing: The type
    initializer for "System.Drawing.SafeNativeMethods" threw an exception."
    Any one has any idea?

    C# code for the component:

    using System;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Runtime.InteropServices;

    namespace ImageProj
    {
     /// <summary>
     /// Summary description for ImageProj.
     /// </summary>

     [GuidAttribute("****GUID****")]

     public class ImageMagic
     {

      private System.Drawing.Bitmap m_bitmap, image;
      public int OriginalWidth, Width;
      public int OriginalHeight, Height;
      public string Format = "JPG";
      private System.Drawing.Size size;
      private string error = "";

      public ImageMagic()
      {
       m_bitmap = null;
       image = null;
      }

      [DispId(1)]

      public int Save(string strFileName)
      {
       size = new Size(Width, Height);
       image = new Bitmap(m_bitmap, size);

       try
       {
        switch(Format)
        {
         case "BMP":
          image.Save(strFileName, System.Drawing.Imaging.ImageFormat.Bmp);
          break;

         case "JPG":
          image.Save(strFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
          break;

         case "GIF":
          image.Save(strFileName, System.Drawing.Imaging.ImageFormat.Gif);
          break;

         case "TIF":
          image.Save(strFileName, System.Drawing.Imaging.ImageFormat.Tiff);
          break;
        }
       }
       catch (Exception e)
       {
        error = "Error: " + e;
        return 2;
       }
       return -1;

      }

      [DispId(2)]

      public int Open(string strFileName)
      {
       try
       {
        m_bitmap = new Bitmap(strFileName);
        OriginalWidth = m_bitmap.Width;
        OriginalHeight = m_bitmap.Height;
       }
       catch (Exception e)
       {
        error = "Error: " + e;
        return 1;
       }
       return -1;
      }
     }
    }

    My asp code:

    <%
    Set Jpeg = Server.CreateObject("ImageProj.ImageMagic")

    Path = "baboon.jpg"

    ' Open source image
    response.write Jpeg.Open(Path)

    response.write Jpeg.OriginalWidth & "<br>"
    response.write Jpeg.OriginalHeight & "<br>"
    Jpeg.Format = "GIF"
    Jpeg.Width = 200
    Jpeg.Height = 200

    response.write Jpeg.Save("baboon.gif")

    set Jpeg = nothing

    response.write "done"
    %>

    I have tested with images in the same folder with a vbs script similar
to
    the above asp code, and it worked.

    Thanks,
    Tim



Tue, 30 Nov 2004 17:03:22 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Using a COM object in .NET that calls back to the invoking .NET class

2. How to Debug .NET Component wrapped as COM

3. COM Component on CE.Net

4. how can I convert com to .net component with a stronge name

5. .net components vs com

6. Writing COM components with C# .NET SDK??

7. Good books on developing com+ components using c#

8. Using CDaoDatabase/CDaoRecordset from inside an ATL/MFC COM Component

9. Problem using COM components in VB

10. How to write COM objects (components) using C#?

11. accessing C# components using VB Com

12. using COM components without type-libraries

 

 
Powered by phpBB® Forum Software