PDA

View Full Version : How to support DDS with dotnet drawing bitmap


david429
04-27-2009, 02:49 PM
(
local bmpDisplayed = dotNetObject "System.Drawing.Bitmap" "c:\test.dds"
local bmpRectangle = dotNetObject "System.Drawing.Rectangle" 0 0 512 512

rollout uiDotNetPictureBox ".NET PictureBox" width:512 height:512
(
dotNetControl uiPictureBox "System.Windows.Forms.PictureBox" pos:[0,0] width:320 height:240

on uiPictureBox Paint senderArg paintEventArgs do
(
Graphics = paintEventArgs.Graphics
Graphics.DrawImage bmpDisplayed bmpRectangle
)
)

try(destroyDialog uiDotNetPictureBox) catch()
createdialog uiDotNetPictureBox style:#(#style_titlebar, #style_border, #style_sysmenu)
)



it's not working with .dds file
local bmpDisplayed = dotNetObject "System.Drawing.Bitmap" "c:\test.dds"

can anyone help me to fix this problem ?

Thanks

ypuech
04-27-2009, 03:33 PM
I think you have to use DirectX Managed (no longer supported now) or XNA to load a DDS bitmap and get it as a .NET bitmap object.

Kameleon
04-27-2009, 06:03 PM
Here's what's probably the worst way to do it :P

(

rollout uiDotNetPictureBox ".NET PictureBox" width:512 height:512
(
button btn_doit "You can do it!"
dotNetControl uiPictureBox "System.Windows.Forms.PictureBox" width:320 height:240

on btn_doit pressed do
(
local bmpRectangle = dotNetObject "System.Drawing.Rectangle" 0 0 512 512

theBitmap=openBitmap @"C:\test.dds"
theImage=dotnetObject "System.Drawing.Bitmap" theBitmap.Width theBitmap.Height
for i=0 to theBitmap.Width-1 do
(
for j=0 to theBitmap.Height-1 do
(
bWH=getPixels theBitmap [i,j] 1
theColor=(dotnetClass "System.Drawing.Color").FromARGB bWH[1].r bWH[1].g bWH[1].b
theImage.SetPixel i j theColor
)
)
close theBitmap
theBitmap=""
uiPictureBox.Image=theImage
)
)

try(destroyDialog uiDotNetPictureBox) catch()
createdialog uiDotNetPictureBox style:#(#style_titlebar, #style_border, #style_sysmenu)
)

LoneRobot
04-27-2009, 06:07 PM
You can also use a third party image library like Freeimage, as it reads DDS files -

http://freeimage.sourceforge.net/

If you download the FreeimageNet wrapper dll, you can use it alongside the (unmanaged) Freeimage library. One great thing to note is this library will allow you to load formats that were previously incompatible with Windows/DotNet, most notably TARGAs.

Note you will need the Freeimage.dll in the same directory as the FreeimageNet.dll


dotnet.loadassembly ((getdir#scripts)+"\LoneRobot\ClassLib\FreeImageNet")

Freeimage = Dotnetclass "FreeImageAPI.Freeimage"
LoadFlags = Dotnetclass "FreeImageAPI.FREE_IMAGE_LOAD_FLAGS"
ImageFormat = Dotnetclass "FreeImageAPI.FREE_IMAGE_FORMAT"

-- load a DDS as a .Net bitmap
DDSfile = Freeimage.LoadBitmap ((getdir#scripts)+@"\logo.dds") LoadFlags.default ImageFormat.FIF_DDS

-- also, you could load a TGA as a .Net bitmap if you liked.
-- DDSfile = Freeimage.LoadBitmap ((getdir#scripts)+@"\testtga.tga") LoadFlags.default ImageFormat.FIF_TARGA

clipboard =(dotNetClass "System.Windows.Forms.Clipboard")

clipboard.setimage DDSFile
DDSDotNetImage = getclipboardBitmap()
Display DDSDotNetImage

ypuech
04-27-2009, 08:53 PM
Artur: It's the worst way because of the speed. It's very slow because of getPixels and setPixels etc...

Pete: I didn't knew FreeimageNet library. That looks very interesting to load bitmap types not supported by the framework.

LoneRobot
04-27-2009, 09:19 PM
Hey Yannick,

I've been using it for a while, and had purchased a .net wrapper for it (Advanced Image library). Freeimage is fast! This was before the C# wrapper on the site was released. Freeimage is x86 only on the site but I found that somewhere it had been compiled for X64. Haven't had the time to test it with that yet, my X64 machine is standing in as my media center at the moment!

Yes, it does bridge the gap in formats, with the notable exception of RPF/RLA, which is the only thing to take the shine off it.

ypuech
04-27-2009, 09:26 PM
Thanks for the informations Pete.

Gravey
04-28-2009, 12:59 AM
Thanks for sharing this resource Pete. I've been looking for a decent image library for a while. I find it really annoying that standard .NET requires you to use the System.Drawing.Color to get/set pixels because each channel is reduced to a byte instead of a ushort for 16bpc images. Will be checking this out for sure.

ypuech
04-28-2009, 06:32 AM
The best way to read a bitmap with .NET is to get its pixel buffer.

LoneRobot
04-28-2009, 07:03 AM
Thanks for sharing this resource Pete. I've been looking for a decent image library for a while. I find it really annoying that standard .NET requires you to use the System.Drawing.Color to get/set pixels because each channel is reduced to a byte instead of a ushort for 16bpc images. Will be checking this out for sure.

glad to be of help. One thing to remember is if you are not using the loadbitmap method, you are opening a freeimage bitmap, which is where all the adjustments and filters can be applied to a low level object. It's a great deal faster than most other methods i've tried. Perhaps i'll write an article if i get time.

Kameleon
04-28-2009, 08:54 AM
Yeah, it's way slow. I've found the FreeImage and the DevIL libraries but for some reason my brain and max didnt want to work together, I was doing dotnet.loadassembly "C:\FreeImageNet.dll" and it was giving me an error... dont know why, I've created some classes and added the .dll in the end and it worked, but as I've seen in LoneRobot example he didnt used it and it seems to work... Well, maybe next time :D Cheers all.

LoneRobot
04-28-2009, 09:10 AM
hey art, i think both ways work, I've had the same issue on occasion too however, have got into the habit of typing the assembly name exactly as a result.

You had any luck compiling ImageMagick? there's supposed to be a .net wrapper for that but i'm buggered if i can make it work, even within VS. :shrug:That library will however open RLA so i'd like to see it work within Max.

david429
04-28-2009, 01:08 PM
Hey Pete , thanks for your share

but it's not woking with max
---------------------------------------------------------------------------------------
dotnet.loadassembly "C:\FreeImageNET" -- work
Freeimage = Dotnetclass "FreeImageAPI.Freeimage" -- work
LoadFlags = Dotnetclass "FreeImageAPI.FREE_IMAGE_LOAD_FLAGS" -- work
ImageFormat = Dotnetclass "FreeImageAPI.FREE_IMAGE_FORMAT" -- work

-- load a DDS as a .Net bitmap
DDSfile = Freeimage.LoadBitmap "C:\test.dds" LoadFlags.default ImageFormat.FIF_DDS --Not Work

clipboard =(dotNetClass "System.Windows.Forms.Clipboard")

clipboard.setimage DDSFile
DDSDotNetImage = getclipboardBitmap()
Display DDSDotNetImage
--------------------------------------------------------------------------------------

I have no idea what's wrong ,

Actually , I would want to show the bitmap texture in rollout bitmap
but the bitmap did not resize as same as the rollout size

Does anyone have any idea to make it work ?

Thank you guys

LoneRobot
04-28-2009, 01:31 PM
you need to download the freeimage.dll as well as the FreeimageNet.dll from the site I posted, this needs to be in your C: location also, as the Net.dll is a wrapper for this. If you have that, then are you running X64?

Kameleon
04-28-2009, 01:50 PM
Hey Pete, I'm still having some trouble with DevIL.NET, I've managed to get FreeImageNet to work but this one I cant, can you take a look at it?

http://www.mastropaolo.com/devildotnet/

I still got this error: -- Runtime error: dotNet runtime exception: Invalid directory on URL.

david429
04-28-2009, 02:04 PM
Pete , Thank you very much, it's work
I was not copy the freeimage.dll to C:\


dotnet.loadassembly "C:\FreeImageNet"

Freeimage = Dotnetclass "FreeImageAPI.Freeimage"
LoadFlags = Dotnetclass "FreeImageAPI.FREE_IMAGE_LOAD_FLAGS"
ImageFormat = Dotnetclass "FreeImageAPI.FREE_IMAGE_FORMAT"

-- load a DDS as a .Net bitmap
DDSfile = Freeimage.LoadBitmap "C:\\test.dds" LoadFlags.default ImageFormat.FIF_DDS

-- also, you could load a TGA as a .Net bitmap if you liked.
-- DDSfile = Freeimage.LoadBitmap ((getdir#scripts)+@"\testtga.tga") LoadFlags.default ImageFormat.FIF_TARGA

bmpDisplayed = dotNetObject "System.Drawing.Bitmap" DDSfile
bmpRectangle = dotNetObject "System.Drawing.Rectangle" 0 0 512 512

rollout uiDotNetPictureBox ".NET PictureBox" width:512 height:512
(
dotNetControl uiPictureBox "System.Windows.Forms.PictureBox" pos:[0,0] width:512 height:512

on uiPictureBox Paint senderArg paintEventArgs do
(
Graphics = paintEventArgs.Graphics
Graphics.DrawImage bmpDisplayed bmpRectangle
)
)
createdialog uiDotNetPictureBox style:#(#style_titlebar, #style_border, #style_sysmenu)

LoneRobot
04-28-2009, 02:06 PM
Hi Dave, glad to hear it!

Hey art, you have to remember, Max is using the Devil wrapper, not Devil.dll itself (a bit like in my photoshop interop (http://forums.cgsociety.org/showthread.php?f=98&t=756327) article earlier in the week)

so -

dotnet.loadassembly ((getdir#scripts)+"\LoneRobot\ClassLib\DevIL.NET2")
cDevil = Dotnetclass "Devil.Devil"
img = cDevil.LoadBitmap ((getdir#scripts)+@"\testtga.tga")
clipboard =(dotNetClass "System.Windows.Forms.Clipboard")
clipboard.setimage img
ImgDotNetImage = getclipboardBitmap()
Display ImgDotNetImage

again you need the devil.dll (and possibly ILU.dll) in the same directory to function.

Kameleon
04-28-2009, 04:12 PM
Nope... Still doesnt work, I even placed the files on C:\ and nothing... :\

LoneRobot
04-28-2009, 04:21 PM
you're still getting the loadassembly error?

Kameleon
04-28-2009, 04:42 PM
Yeap, it's really weird, I've also tried placing the files in my #scripts dir and used the same dir struct as you and nothing, same error. I'm on Windows Vista 64 btw.

LoneRobot
04-28-2009, 04:46 PM
hmm, can't test that at the moment on that platform, artur - So no way of knowing. there is an x64 version of Devil now, you have got that build rather than the x86? if so, you could try downloading the source for Devil.net and recompiling it for the X64 platform, i know that wrapper existed when there was only a x86 version of devil.

edit - just checked on the devil site and i cant see a precompiled binary for the x64 platform, the link that should be that just goes to the source. I dont know C++ at all, so cant figure out how to compile it. I've emailed Denton Woods to see if there is one available.

one last thing, have you tried downloading the projects for devil.net -

http://www.mastropaolo.com/files/DevILDotNet_sources.zip

if you can build these and run them in x86 space and max still doesnt want to work then i'm thinking it must be the 64 bit dll issue.

CGTalk Moderation
04-28-2009, 04:46 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.