I have an simple test. When it is solved, my problem is solved too. When working with small images, the graphics interpolation does bad work.
Please check out if you know how to fix the problem that the result image in the following code does ignore second half of image to draw. Draw something on the image by using loadimage from JPG or whatever you want.
Dim GrayImage as system.drawing.Bitmap(640,480)
Dim bmTmp As New System.Drawing.Bitmap(GrayImage.Width, 1)
Using gr As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bmTmp)
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear
gr.DrawImage(GrayImage, New System.Drawing.Rectangle(0, 0, bmTmp.Width, bmTmp.Height), New System.Drawing.Rectangle(0, 0, GrayImage.Width - 0, GrayImage.Height - 0), System.Drawing.GraphicsUnit.Pixel)
End Using
GrayImage = New System.Drawing.Bitmap(GrayImage.Width, GrayImage.Height, GrayImage.PixelFormat)
Using gr As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(GrayImage)
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor
gr.DrawImage(bmTmp, New System.Drawing.Rectangle(0, 0, GrayImage.Width, GrayImage.Height ), New System.Drawing.Rectangle(0, 0, bmTmp.Width - 0, bmTmp.Height - 0), System.Drawing.GraphicsUnit.Pixel)
End Using
Download original Source here: http://www.goldengel.ch/temp/Source01%20one%20Pixel.jpg (one Pixel height image)
The second half vertical is not drawn by using the DrawImage methode. I want to have the image as result as you see on first picture. Stretched image with source on whole content.
* DOWNLOAD* Download here full working VS2010 VB.Net demo project:
Please try to add:
to your last clause. GDI magic I guess :)
Hope this helps!
As @EmirAkaydin said in his answer, your problem lies with the interpolation. I suspect his answer about it only being one pixel high, conflicting with Microsoft's resize algorithm, is correct.
I have a two step solution for you. Unless you want to write your own resize code (I didn't want to) that resizes exactly as you want, you could still use the
Graphics.DrawImage
function to at least resize the width of your image, but only the width. Then you can manipulate the pixel data directly and copy each that first valid line, for the entire height of the image.You can replace your
DoDemo
code with the following (I don't use VB, so I'm not sure about the coding style; it does work, however):EDIT:
Interestingly enough, the code @FredrikJohansson posted will work as well:
right before you draw the image. I'm going to leave this code and answer here, in case anyone ever wants to see it, but it looks like he answered your question in an easier way :)
I had a similar problem which I had solved by resizing the original image. I guess this is an expected behaviour, not a bug (usual MS explanation but for this case it might be right :)). Some interpolation algorithms reqiure more than one pixel line (applies to both axes) to work correctly. If there is no second line, they may interpolate to an empty line which causes that problem above. You may change you image width/height to 2 pixels minimum or use proper interpolation methods for single line images.
Have you tried NearestNeighbor interpolation?