Today we've released a new updated version of the ImageBox control, with a nice collection of enhancements and a few bug fixes.

Full change log for this update:

Changes and new features

  • Added NuGet package
  • Added a license file to hopefully cut down on questions about usage. The ImageBox control is licensed under the MIT license, allowing you free reign to use it in your projects, commercial or otherwise. See imagebox-license.txt for the full text.
  • Added a new SizeMode property. This allows you to switch between Normal, Fit and Stretch modes. Stretch is a new mode for the ImageBox, and acts similar to existing Fit functionality except the aspect ratio is not preserved.
  • The SizeToFit property has been marked as deprecated and should no longer be used. The SizeMode property has a Fit value that should be used instead. Setting the SizeToFit property will now manipulate SizeMode instead.
  • Added a new CenterPoint property. This property returns the pixel at the center of the current image viewport.
  • Added a bunch of missing XML comments documentation.
  • Added new overloads for most methods that accepted a source Rectangle, Point or Size to also accept float and int arguments.
  • Added a new Zoomed event that uses new ImageBoxZoomEventArgs arguments. This new event allows you to tell if the zoom was in or out, how it was raised, and current and previous zoom values. Not hugely thrilled with how aspects of this change has been internally implemented, so implementation methods are private rather than virtual so I can change them without affecting the signature.
  • Added new CenterToImage method which resets the viewport to be centered of the image, in the same way as zooming via the keyboard used to work.
  • Added support for animated GIF's, thanks to a contribution from Eggy. Note animations only play at runtime, not design time.
  • The Text and Font properties are now available and, if set, will be displayed in the control. You can use the ForeColor, TextBackColor, TextAlign, TextDisplayMode and ScaleText properties to determine how the text will be rendered.
  • A new DrawLabel method that performs text drawing is available for use by custom implementations or virtual modes.

Demonstration Changes

  • Added a new Scaled Adornments demonstration, showing how easy it is to add custom drawing that is scaled and positioned appropriately.
  • Added a new Switch Image During Zoom demonstration, a demo with an unwieldy name that shows how to switch out a low resolution image with a higher detailed one as you zoom into an ImageBox.
  • Added new Text and Size Mode demonstrations.

Bug Fixes

  • Zooming in and out with the keyboard now keeps the view centered to the same pixel that was centered prior to the zoom

  • Zooming in and out with the keyboard is now correctly disabled if the AllowZoom property is False, or the SizeMode property is a value other than Normal. This means keyboard behaviour now matches mouse behaviour.

  • If the mouse wheel was rapidly spun (thus having a multiple of the base delta), the Zoom property was only adjusted once

  • Setting the GridScale property to None rendered the default Small grid. Using a scale of None now correctly just fills the grid area with a solid brush from the GridColor property.

  • The MouseWheel event is now available

  • Layout changes no longer occur if the AllowPainting property is false through use of the BeginUpdate method.

  • Fixed various documentation errors

Downloads

As usual, either grab the source from GitHub, from the link below, or make use of the NuGet package!


Like what you're reading? Perhaps you like to buy us a coffee?

Donate via Buy Me a Coffee

Donate via PayPal


Files


Comments

# Richard R.

How can I include the ImageBox class/control into my current solution (c# - WinForm) eliminating the need for the DLL dependency? I really want to find a way for this approach as distributing an additional DLL would be problematic.

Reply

# Richard Moss

Hello,

Although the control has grown a bit (or a lot :)) beyond it's more humble beginnings, you can still just add all the files in the solution to your own project and go from there. You could probably exclude some files and delete code that you don't need. (For example the new Zoomed event, if you don't need it, delete it and drop the related event args class and support enum classes. I planned on adding conditional constants to the next update, but deleting code is just as easy!)

However, if you can I'd recommend you stick with a pre-built binary - the NuGet package will be updated with each new version. But, as you have the source you can embed it in your project and tweak it for your needs.

Hope this helps!

Regards; Richard Moss

Reply

# Richard R.

Love the control! As for the question, I found that you need to get all the needed files into your own project. Compile it - fix - compile until there are no errors AND then use the toolbox feature to "Choose Items". Browse to the location of the EXE file your compile just created and select that file. ( This is just like if you had worked with a class lib and created a DLL :) ) Click on the add/ok button and the toolbox will find the control and add it to the toolbox. After that, adding it to my form and everything else went great! I expect that we will work with this control and be able to contribute back soon with new additions and improvements. This is a fantastic control! Thank You!

Reply

# Richard Moss

Hello,

There ought not to be any compile errors, what problems did you have? In regards to the toolbox, I don't normally include it directly in the application source, but I do include it in a class library which is always added in source form to the project - Visual Studio automatically populates the toolbox with items in this scenario.

However, I'm glad you've got it embedded and are finding it useful. Good luck with your project!

Regards; Richard Moss

Reply

# Richard R

No problems with ImageBox, just trying to make clear that you should have a clean compile before trying to add the control to the toolbox. For my particular project, I needed it embedded and the end result was great! Thanks again!

Reply

# Ravi Bhavnani

First of all, thanks for an amazing control and a full-featured demo app!

Would it be possible to add the ability to resize the selection rectangle (a la the ye olde MFC CRectTracker)?

Thanks,

--Ravi B

Reply

# Richard Moss

Ravi,

It is an idea which has crossed my mind a few times, but at the same time it's expanding functionality beyond just the displaying of images, which at the end of the day is what the control is for. Still, it is in the back of my mind as something which would be useful so sooner or later I'll add it (otherwise why did I add selection in the first place!), or someone else will contribute it :)

Regards; Richard Moss

Reply

# Misiu

First of all this is awesome control and awesome API to convert pdf's into images. I'm right now creating application (university project) to open multiple files (document viewer) and I was searching for a way to display PDF's. Thanks to Your solution this is possible! I integrated Your code into my application in minutes. Everything works nice, I have just 2 tiny comments:

  1. When loading pages inside Your demo app I noticed that whole application is freezing for a second. Do You think it would be possible to do that in separate thread? Show some kind of loading indicator and hide after everything get loaded? This way UI won't freeze. I'm building my project using .NET 4 so Tasks may be handy :)
  2. Second thing is mouse zooming, right now is is zooming to top left corner, could that be changed to zoom to mouse position? If I need to read informations that are in bottom-right corner and I zoom in I must use scrollbars to scroll to that place. I suppose that I could point mouse there and using wheel zoom to that region. Maybe this is configurable, but I didn't found it yet.

Ones more thanks for this great code and best wishes.

Reply

# Richard Moss

Hello,

Thanks for your comments, I'm glad you're finding the code useful.

In regards to your first comment - I assume you're talking about another article on this site which describes how to use the ImageBox + GhostScript in order to display PDF's? It should be perfectly possibly to get a Task to do this work, just modify the code that calls the PDF conversion. Not the most helpful answer but off the top of my head I can't remember how I put together the PDF view code!

For comment two, this is an old bug in the ImageBox - did you try the latest version of the code (you can get this from the GitHub page - https://github.com/cyotek/Cyotek.Windows.Forms.ImageBox)

Regards; Richard Moss

Reply

# Misiu

Thanks Richard :) I was using old version of ImageBox that was with CodeProject article. After adding Your control using NuGet and some small changes everything is working fine :)

Btw any news about GhostScriptAPI? at line 659 You have TODO item about wrong combination of params. In my case I get this error when I try to open very large pdf (2900 x 6500 pixels).

Again many thanks for this awesome control and API.

Reply

# Richard Moss

I thought that was the case, glad you're up and running with the latest code now.

In regards to the GhostScript library, the project I was going to use it for has been sitting on the backburner for some time now so I haven't been back to it. It's unlikely that I'll get to it any time soon as I have so many other half complete projects I'd like to get done! Hopefully I'll get the chance to look at it sooner than later though.

Regards; Richard Moss

Reply

# Misiu

Hi again, I did some tests and it looks like error was because I had very long file name with spaces and dashes. After changing name to shorter everything works fine :)

Reply

# Richard Moss

Great, thanks for letting me know! That's something the library could automatically work around with a bit of cleverness... if I ever pick the project up again I'll look into it!

Regards; Richard Moss

Reply

# Dave Gordon

Hi, Excellent ImageBox; which I am using in a series of articles over on MSDN.

When we are zoomed in, the Mouse XY coordinates do not relate to the actual Zoomed image coordinates. So we are unable to detect what colour the mouse is hovering over as the mouseXY coordinates differ from the virtual coordinates of the zoom.

How do we get the colour of the pixel the mouse is over regardless of the zoom or panning of the image within the ImageBox container?

Thanks again Richard!

Reply

# Richard Moss

Glad you're finding it useful! You can use the PointToImage to convert a point such as that from MouseEventArgs into the appropriate location within the image. You can then use that result to query the bitmap either via Bitmap.GetPixel or via the direct pixels if you have already extracted and stored these via Bitmap.Lockbits. (The former is going to be slow, especially if you are going to be updating as you move the mouse around, whilst the latter is complicated to explain especially if you use pointers). Both of these assume you are using a Bitmap object even though the ImageBox uses the more generic Image instead. Although now that I think, I've never tested (or used for that matter) a metafile.

PS: I noticed in your article that you're linking to an older version of the code. Might be better to link to the GitHub page for this project instead (https://github.com/cyotek/Cyotek.Windows.Forms.ImageBox) or it's NuGet package (https://www.nuget.org/packages/CyotekImageBox/). Hmm, or maybe I should add an overview page to this site instead of scattered blog posts.

Hope that helps! Regards

Reply

# schorges

Nice work! Very good job, thanks!

But i have one querstion: how do i hide the scrollbar, or set the scrollbar visibile to false?

Reply

# Richard Moss

Basically, you can't. The information needed to handle scroll position is not available if the scrollbars are destroyed. (See this GitHub issue)

I sat down a couple of weeks ago to rework the basic underpinnings of the ImageBox with the intention to fit this, but it's a breaking changed to compatibility to I haven't created binaries for it yet (nor in truth have I finished testing it). If you want the latest source which should fix this, you can get it from the GitHub page.

Hope this helps!

Regards; Richard Moss

Reply

# Essam Mohamed Zaki

Multiple region selection First thanks for this nice project , I have a project which need to select multiple regions from the loaded image, the current image control only select one region , would you guide me to add multiple regions selection and retrieve the selected regions

Reply