Tag: WsImage

  • EnsureMaximumSize and GetThumbnail

    The WsImage class wraps a System.Drawing.Bitmap object. It has two methods to control size called EnsureMaximumSize and GetThumbnail. These are the differences:

    EnsureMaximumSize transforms the bitmap that is encapsulated in the WsImage object, GetThumbnail returns a bitmap and leaves the encapsulated bitmap unchanged.

    EnsureMaximumSize maintains the aspect ratio of the picture and the bitmap object, GetThumbnail returns a rectangular bitmap object with a the original picture drawn onto it. The picture still has maintained aspect radio. You can pass a brush object to control the background, or pass null (Nothing) if you want the background to be transparent.

    The similarity is that both EnsureMaximumSize and GetThumbnail only takes one integer parameter to control size. That integer represents the width if the width is larger than the height, otherwise it represents the height.

    If you load an image sized 800×600 and pass 700 to the EnsureMaximumSize function, it will return True and the image will be sized 700×525. If you load an image sized 300×400 and pass 350 to the EnsureMaximumSize function, it will return True and the Image will be sized 262×350 pixels. If you load an image sized 300×400 and pass 450 to the EnsureMaximumSize function, it will return False and the image will not be resized.

    Download WsImage.

  • Images: EnsureMaximumSize

    I have added a new function for resizing with maintained proportions to my image class WsImage.Image. The name is EnsureMaximumSize, and it takes one integer (the maximum width or height) and returns a boolean. The return value is True if the image size changed, otherwise the return value is False.

    Imagine that you load an image that is 600 pixels high, and 400 pixels wide. If you call the EnsureMaximumSize function and pass 500 to the parameter, the image will be resized so the new height is 500, and the width is changed so that the width/height ratio is maintained. The return value will be True.

    If you would have passed the value 1000 in this situation, the image would not be resized, and the return value would be False.

    This code will produce a 800×600 pixel image, and draw both the 800×600 version and the 640×480 version of it. (I wrote this code in the Click event of a Form.)

    'In the Click event of a form, create an image.
    Dim X As New WsImage.Image(800, 600)
    
    'Create a Graphics object to enable drawing on the image.
    Using G As Graphics = X.CreateGraphics()
    
    	'Draw a white background and a black ellipse.
    	G.FillRectangle(Brushes.White, 0, 0, 800, 600)
    	G.FillEllipse(Brushes.Black, 0, 0, 800, 600)
    
    End Using
    
    'Create a Graphics object to be able to draw on the form.
    Using G As Graphics = Me.CreateGraphics()
    
    	'Draw the original bitmap to the form.
    	G.DrawImage(X.GetBitmap(), 0, 0)
    
    	'Tint the background.
    	Using Tint As New SolidBrush(Color.FromArgb(127, 0, 0, 0))
    		G.FillRectangle(Tint, 0, 0, 800, 600)
    	End Using
    
    	'Do a 640 x 480 version, and draw it.
    	X.EnsureMaximumSize(640)
    	G.DrawImage(X.GetBitmap(), 40, 40)
    End Using

    Check the WsImage tag for more information on this class. You can download it here (if you use it, give me credit).

  • WsImage.Image

    For quite some time, I have had the need for some extra high level functionality on top of the .NET Bitmap class. I want to be able to do some basic image manipulation and I want the typical functionality that you would expect from a content management system, like cropping and resizing. This is why I have started to build the WsImage.Image class. And I mean that I really just have started. Today. It is written in Visual Basic, but I have avoided the “VB only” features, like parameterized properties, so it works perfectly in a C#-project or Managed C++-project.

    It is a disposable object, use the Using keyword to create it (as you would with any disposable object). One constructor takes a desired with and height, and another takes the filename of an existing image.

    The WsImage.Image class wraps the .NET Bitmap class, and here is the current API (believe me, this is going to change in the future):

    CreateGraphics
    Returns a Graphics object.

    CropToRectangle
    Makes the image rectangular. For a image that is wider than it is high, the hight will not change, and the width will be cropped to match the height. The cropping is centered.

    GetBitmap
    This simply returns the Bitmap object that is contained in the WsImage.Image object. Do not dispose this object, just forget it when you’re done with it.

    GetProportions
    Returns a Size object that describes the proportions of the image. For example, an image sized 1000×1000 pixel will return a size object with the value 1×1, an image sized 17×11 pixels will return a size object with the value 17×11, an image sized 800×600 pixels will return a size object with the value 4×3 and so on.

    GetSize
    Returns the size of the image.

    GetThumbnail
    Returns a thumbnail as a .NET Bitmap. Currently, there is no overload for actually requesting the size of the thumbnail, it simply gives you a 100 pixels wide version of image. If the image is less than 100 pixels wide, black border is added – zooming will not occur. Dispose the returned Bitmap when you are finished with it.

    Yes, you can download the compiled class here, but if you’re not deadly interested in it, you could wait until it has a few more features in it. Like I said, I started today. If you do use it, remember to credit me in the about box.