all groups > vj# > january 2005 >
You're in the

vj#

group:

System.Drawing.Bitmap to java.awt.Image?


System.Drawing.Bitmap to java.awt.Image? alex
1/9/2005 7:17:02 PM
vj#:
I'm loading a .gif image from an assembly via ResourceManager.getObject,
which returns System.Drawing.Bitmap. But I want to use it as the image on a
JButton. Does anyone have a J# example that converts System.Drawing.Bitmap
to java.awt.Image, in memory? (I know I could write the .gif to a file, and
read it back in via Toolkit.getImage, but thats not what I want...)





RE: System.Drawing.Bitmap to java.awt.Image? alex
1/10/2005 7:17:10 AM
Well here's one approach that works:

1. in the projects's .resx file, change the type of the .gif file from
System.Drawing.Bitmap to System.Byte[]

2. Use ResourceManager.GetObject() to get the .gif data as ubyte[]:

ubyte[] b = (ubyte[]) resMgr.GetObject("MyGif");

3. Create java.awt.Image from .gif data:

byte[] b2 = new byte[b.length];
for (int i=0; i < b.length; ++i)
b2[i] = (byte) b[i];

Image image = Toolkit.getDefaultToolkit().createImage(b2);
Re: System.Drawing.Bitmap to java.awt.Image? alex
1/10/2005 2:01:02 PM
Thanks. The suppUI lib does include java.awt.BufferedImage, so your
suggestion could work.

ps. Posts 1 & 2 are both mine -- I answered my own question, but am always
open to better suggestions.

[quoted text, click to view]
Re: System.Drawing.Bitmap to java.awt.Image? alex
1/10/2005 2:05:02 PM
Oops, I' wrong. The J# suppUI lib does NOT contain
java.awt.image.BufferedImage

[quoted text, click to view]
Re: System.Drawing.Bitmap to java.awt.Image? George Birbilis [MVP J#] [9880]
1/10/2005 6:27:42 PM
if supplemental UI library implements "BufferedImage" you could create one
in memory, use appropriate method to get a graphics object from it and then
draw your stuff on that graphics object somehow - just a thought (alex has
suggested some better way I think)

I think you might not even need BufferedImage but use a normal Image instead
in J# (in Sun Java offscreen buffers can't be created with Image unless you
relate them to a component that's visible - that was almost the only choice
in Java1.1 [using an array can be quite slow] - till BufferedImage came at
Java1.2+)

[quoted text, click to view]

Re: System.Drawing.Bitmap to java.awt.Image? Lars-Inge Tønnessen [VJ# MVP]
1/11/2005 6:19:32 PM

Cool solution. =:o)


Best Regards,
Lars-Inge Tønnessen

Re: System.Drawing.Bitmap to java.awt.Image? George Birbilis [MVP J#] [9880]
1/28/2005 12:42:32 AM
[quoted text, click to view]

no, but according to J# team the java.awt.Image implementation that you get
from <Canvas>.getImage (hope I remember the method OK) is an offscreen image

in Java1.1.x [maybe at Java2 too] that method needed your component to first
be added to a containment hierarchy that has a native peer [a window etc.]
as the ultimate grand-grand-...-parent

so try making a java.awt.Canvas, call getImage on it, then call getGraphics
on that image. Do remember to Dispose/Destroy any Graphics objects and
Images you create yourself (they have appropriate methods). BTW, never
destroy the Graphics object you get passed as a param to a "paint" method,
that's handled by the JVM (oops, by J# libs in the .NET case ;o)

if above stuff doesn't work (see blank image), try adding the Canvas to some
java.awt.Frame or java.awt.Window first, even if that one isn't displayed
(think you don't need to display it, just creating window/frame will make
necessery native peer). However this thing shouldn't be needed according to
some older discussion I had with J# team regarding adding BufferedImage
(having BI class would be cool anyway though)

cheers,
George


--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
George Birbilis <birbilis@kagi.com>
Microsoft MVP J# for 2004, 2005
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ QuickTime controls (Delphi & ActiveX: VB, PowerPoint, .NET)
+ Plugs (InterProcess/Internet communication)
+ TranXform (VB6 form to ASP.net WebForm convertion)
http://www.kagi.com/birbilis
+ Robotics
http://www.mech.upatras.gr/~robgroup
.........................................................................

AddThis Social Bookmark Button