PDA

View Full Version : Image display with dotNet?


martinB
02-02-2008, 02:17 PM
I would like to show an image (taken from a bitmap value) in a floating rollout with the ability to zoom into the image and then scroll/pan around. Also, I would like to catch mouse-button-down events.

I haven't used dotNet yet but I wonder whether this would be possible with dotNet, and if so, maybe someone can point me into the right direction?

Thanks!
-- MartinB

RustyKnight
02-03-2008, 07:43 AM
I would like to show an image (taken from a bitmap value) in a floating rollout with the ability to zoom into the image and then scroll/pan around. Also, I would like to catch mouse-button-down events.

I haven't used dotNet yet but I wonder whether this would be possible with dotNet, and if so, maybe someone can point me into the right direction?

Thanks!
-- MartinBI was going to ask "what's wrong with display <bitmap>?" But I see you want it displayed in a rollout as well.

The real question is, can DotNet do this out of the box or not...

Now, there is a "PictureBox" control in "System.Windows.Forms" that MIGHT work, how much control you would get is another issue...

As I read it, it has a "Load(String)" method which "sets the ImageLocation to the specified URL and displays the image indicated".

Now, unfourtantly, support for magnification (and scrollbars) may not be avaliable out of the box for this control, but you could "fake" some of the magnification yourself...But more about that later...

Scrolling could be achieved by placing the picture box inside a ScrollableControl...this is a little more complex, because you are now responsible for the layout (position and size) of the control...

But, basically, you would do something like myScrollableControl.controls.add myPictureBox

As for the magnification...there MIGHT be a way to do use the PictureBox itself...if that fails, you might need to convert the image into a pixel array and scale it yourself :P

But, PictureBox has a method "Scale(SizeF)"

Now SizeF has a number of cool properties, Height and Width been the most important, but Add(...) and Subtract(...) also look interesting...

As I understand the docs, Height and Width are a "factor" of the original size, that would mean that 1.0 is normal, 0.5 is half and 2.0 is double, but you will need to play around with these values to be sure...

NONE OF THIS HAS BEEN TESTED AND IS PURLY SPECULITVE...

Shane

ypuech
02-03-2008, 09:32 AM
I can add two ore more things to what Shane said.

You can also use the "System.Drawing.Bitmap" class to manipulate images. This class is very powerful. I think scrolling would be not so hard to implement using a "System.Windows.Forms.ScrollBar" and intercepting the "MouseWheel" event of the PictureBox for example.

I'll post an example of how to show a bitmap with .NET today and I'll test if we can scroll into the PictureBox. I think this would not take too long time.

EDIT:

Here is the code to display an image with .NET:

(
local bmpDisplayed = dotNetObject "System.Drawing.Bitmap" "image.bmp"
local bmpRectangle = dotNetObject "System.Drawing.Rectangle" 0 0 320 240

rollout uiDotNetPictureBox ".NET PictureBox" width:320 height:240
(
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)
)

RustyKnight
02-03-2008, 12:09 PM
Hay ypuech! That's a very interesting idea! I like it :D

It would mean some extra work with the scroll bars though, but that's not impossible to implement.

How would you go about scaling the image?? This is not a critism, I'm actually interested in knowing some more about dotnet's image handling for future references...you never know when someone is going to ask you to do something like this ;)

Shane

ypuech
02-03-2008, 12:38 PM
How would you go about scaling the image?? This is not a critism, I'm actually interested in knowing some more about dotnet's image handling for future references...you never know when someone is going to ask you to do something like this ;)

That's a good question but I found an answer! :)
I'll try to implement the solution:
Adding zoom-in and zoom-out features requires only one operation; multiplying the height and width of the image by a zoom factor...
So it's something like you previously said.

martinB
02-03-2008, 02:33 PM
Shane and Yannick,

first of all: Thanks very much for your help! This forum is pretty awesome, I am glad I came here after The Area got unuseable!

Your suggestions sound interesting and already fairly close to what I am looking for. Since I have almost no experience with dotNet, some more example code would be fantastic. ;-)

Yannick, in your code you load an image from disk. How would I pass a bitmap value to the dotNet control?

Thanks again!
-- MartinB

ypuech
02-03-2008, 03:15 PM
Yannick, in your code you load an image from disk. How would I pass a bitmap value to the dotNet control?
MAXScript bitmap value or .NET bitmap value ?
In fact it's better for speed to draw the bitmap .NET value in the paint event handler.

EDIT:

Here is my code for zooming without a scroll control:

(
local uiDotNetPictureBoxZoomRollout
local uiDotNetPictureBoxRCmenu

local sRolloutTitle = ".NET PictureBox Zoom"
local zoomFactor = 2.0

local bmpWidth = 320
local bmpHeight = 240
local bmpDisplayed = dotNetObject "System.Drawing.Bitmap" "image.bmp"
local bmpRectangle = dotNetObject "System.Drawing.Rectangle" 0 0 bmpWidth bmpHeight

rcmenu uiDotNetPictureBoxRCmenu
(
subMenu "Zoom"
(
menuItem uiZoom100 "100"
menuItem uiZoom200 "200"
menuItem uiZoom300 "300"
menuItem uiZoom400 "400"
menuItem uiZoom500 "500"
)

on uiZoom100 picked do
(
zoomFactor = 1.0
uiDotNetPictureBoxZoomRollout.updateZoom()
)

on uiZoom200 picked do
(
zoomFactor = 2.0
uiDotNetPictureBoxZoomRollout.updateZoom()
)

on uiZoom300 picked do
(
zoomFactor = 3.0
uiDotNetPictureBoxZoomRollout.updateZoom()
)

on uiZoom400 picked do
(
zoomFactor = 4.0
uiDotNetPictureBoxZoomRollout.updateZoom()
)

on uiZoom500 picked do
(
zoomFactor = 5.0
uiDotNetPictureBoxZoomRollout.updateZoom()
)
)
registerRightClickMenu uiDotNetPictureBoxRCmenu

rollout uiDotNetPictureBoxZoomRollout sRolloutTitle
(
dotNetControl uiPictureBox "System.Windows.Forms.PictureBox" pos:[0,0] width:bmpWidth height:bmpHeight

fn updateZoom =
(
zoomPercent = (zoomFactor * 100) as integer
newWidth = bmpWidth*zoomFactor
newHeight = bmpHeight*zoomFactor
uiDotNetPictureBoxZoomRollout.Width = newWidth
uiDotNetPictureBoxZoomRollout.Height = newHeight
bmpRectangle.Width = newWidth
bmpRectangle.Height = newHeight
uiPictureBox.Width = newWidth
uiPictureBox.Height = newHeight
uiDotNetPictureBoxZoomRollout.Title = sRolloutTitle + " : " + (zoomPercent as string) + "%"
uiPictureBox.Invalidate()
)

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

on uiDotNetPictureBoxZoomRollout open do
(
uiDotNetPictureBoxZoomRollout.Title = sRolloutTitle + " : 100%"
)
)

createdialog uiDotNetPictureBoxZoomRollout menu:uiDotNetPictureBoxRCmenu width:bmpWidth height:bmpHeight style:#(#style_titlebar, #style_border, #style_sysmenu)
uiDotNetPictureBoxRCmenu.rolloutCtrl = uiDotNetPictureBoxZoomRollout
)

martinB
02-03-2008, 07:04 PM
MAXScript bitmap value or .NET bitmap value ?

A MAXScript bitmap value, in fact. It could be converted to a .NET one (if one knew how to do so....).

Here is my code for zooming without a scroll control:

Thanks, very helpful indeed. That's a lot of code, thanks a lot for taking the time to write it!
Curious that you need to resize three different things at the same time.

In the end, I would like to be able to a.) resize the window and get scrollbars is necessary, and b.) zoom into the image without changing the overall window size.

I get the resizing bit by adding #style_resizing to the createdialog call, and the window is not resized when I comment the two uiDotNetPictureBoxZoomRollout lines in updateZoom().

But how do I get scrollbars?

-- MartinB

ypuech
02-03-2008, 07:50 PM
A MAXScript bitmap value, in fact. It could be converted to a .NET one (if one knew how to do so....).
I have code to do that. You can think of an algorithm like this:
Get pixel color from MAXScript bitmap -> Set it to .NET bitmap
This is pretty slow in MAXScript but fast in an assembly written in C# or VB.NET. So to convert from a MAXScript to a .NET bitmap you need to use an intermediate method that can return a .NET bitmap from an integer pixel color buffer for example.

But how do I get scrollbars?
You have to use a HScrollBar and VScrollBar from "System.Windows.Forms". Easy to say, but not to implement...

RustyKnight
02-03-2008, 10:39 PM
Adding zoom-in and zoom-out features requires only one operation; multiplying the height and width of the image by a zoom factor...
So it's something like you previously said.Nooo, you can't do that, it's to simple!! There has to be a harder, more convoluted method...it is microsoft after all!!

martinB
02-04-2008, 12:29 AM
FWIW, I came across the .autoScroll property of PictureBox.
I think this will take care of the scrollbars?

-- MartinB

diffx
01-19-2009, 03:42 PM
I have code to do that. You can think of an algorithm like this:
Get pixel color from MAXScript bitmap -> Set it to .NET bitmap
This is pretty slow in MAXScript but fast in an assembly written in C# or VB.NET. So to convert from a MAXScript to a .NET bitmap you need to use an intermediate method that can return a .NET bitmap from an integer pixel color buffer for example.

Sorry for digging up a really old thread here, but this is exactly what i need.
I'm rendering preview bitmaps (as explained by Bobo here (http://forums.cgsociety.org/showthread.php?f=98&t=318191) ) and I would like them to display in my .net listview (xptable). I've got the previews in an array.

I could save the file to a tempdir and then read it with imageClass.fromFile, which creates a temp folder/file on the users disk which I don't want.

Can anyone explain how I can do the Maxscript bitmap -> .NET bitmap as suggested by ypuech?

ypuech
01-19-2009, 03:58 PM
See this post with code sample:

http://forums.cgsociety.org/showpost.php?p=5335327&postcount=10

You can easily and very quickly convert a .NET bitmap to MAXScript and vice versa using the windows clipboard : SetClipboardBitmap(), GetClipboardBitmap() and System.Windows.Forms.Clipboard.SetImage() and System.Windows.Forms.Clipboard.GetImage().

LoneRobot
01-19-2009, 04:16 PM
Hi Yannick, hope you're well.

I've been mulling this one over for a while, and despite the clipboard swapperoo method, which works great, I've been wondering of there could be another way. For example both MXS and .net have ways of using memory streams to store info, and dotnet has image.fromstream as a method. Now as i know nothing of C++ or the SDK is there something about how max packages a bitmap that dotnet cant get at, even at a lower level?

diffx
01-19-2009, 04:24 PM
See this post with code sample:

http://forums.cgsociety.org/showpost.php?p=5335327&postcount=10

You can easily and very quickly convert a .NET bitmap to MAXScript and vice versa using the windows clipboard : SetClipboardBitmap(), GetClipboardBitmap() and System.Windows.Forms.Clipboard.SetImage() and System.Windows.Forms.Clipboard.GetImage().

Fantastic, works like a charm! Thanks for the quick reply.

CGTalk Moderation
01-19-2009, 04:24 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.