Hi all,
When resizing a bitmap, is it possible to make the resizing alised, because otherwiseI get smooth tones which I don't want? Because I need the pure colors.
Thanks,
aw | | Joan Venge Tuesday, May 29, 2007 9:56 PM |
Dim MyImage As New Bitmap("C:\untitled.bmp")
Dim factor As Double = 0.5
Dim MyThumbNail As Bitmap
MyThumbNail = New Bitmap(MyImage, New Size(MyImage.Size.Width * factor, MyImage.Size.Height * factor))
MyThumbNail .Save( "C:\Thumb.bmp")
But this is even more elegant:
Dim MyImg As New Bitmap("C:\untitled.bmp")
Dim Factor as double = 0.5
Dim MyThumb As Image
MyThumb = MyImg.GetThumbnailImage(MyImg.Size.Width * factor, MyImg.Size.Height * factor, AddressOf ThumbNailAbort, Nothing)
MyThumb.Save( "C:\k.bmp")
Private Function ThumbNailAbort() As Boolean
'Do Nothing Here
End Function
Hope this helps | | Karl Davies-Barrett - MCT Tuesday, May 29, 2007 11:16 PM | Sadly, the Graphics class is missing a "do not interpolate at all" mode. You'll get blending artifacts, no matter which Interpolation mode you try. Here's a solution, it works by resizing with the old GDI StretchBlt() function.
public static Image ResizeImage(Image srce, int width, int height) { Bitmap dest = new Bitmap(srce, width, height); Graphics grDest = Graphics.FromImage(dest); IntPtr hdcDest = grDest.GetHdc(); Graphics grSrce = Graphics.FromImage(srce); IntPtr hdcSrce = grSrce.GetHdc(); StretchBlt(hdcDest, 0, 0, width, height, hdcSrce, 0, 0, srce.Width, srce.Height, SRCCOPY); grSrce.ReleaseHdc(hdcSrce); grDest.ReleaseHdc(hdcDest); grSrce.Dispose(); grDest.Dispose(); return dest; } [System.Runtime.InteropServices.DllImport("gdi32.dll")] static extern bool StretchBlt(IntPtr hdcDest, int nXOriginDest, int nYOriginDest, int nWidthDest, int nHeightDest, IntPtr hdcSrc, int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc, int dwRop); private static int SRCCOPY = 0xCC0020;
| | nobugz Wednesday, May 30, 2007 12:02 AM | I thought you had a Bitmap?
Get the graphics from the Bitmap and set the interpolation mode to nearest neighbor.
Code Snippet
Dim g As Graphics = Graphics.FromImage(YourBitmap)
g.InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor
g.DrawImage(Yourbitmap, 0, 0, NewWidth, NewHeight)
| | JohnWein Wednesday, May 30, 2007 8:38 AM | Ouch, my code doesn't work. It was bound to happen but I'll now have to drop the "no" prefix on my nick. Stupidly, I tested it by shrinking the image, not by growing it. As yet, I have no clue what might be wrong, StretchBlt() returns TRUE to indicate it worked but it doesn't actually copy the bitmap. If anybody sees a mistake, please shout. Using a compatible DC is what I need to try next, I'll need some time for that.
| | nobugz Thursday, May 31, 2007 9:08 PM | Yeh. Well my code is wrong it should be:
Code Snippet
Dim NewWidth, NewHeight As Int32
Dim NewBitmap As New Bitmap(NewWidth, NewHeight)
Dim g As Graphics = Graphics.FromImage(NewBitmap)
g.InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor
g.DrawImage(Yourbitmap, 0, 0, NewWidth, NewHeight)
Edit:
Or, using Instant C#
Code Snippet
Int32 NewWidth = 0;
Int32 NewHeight = 0;
Bitmap NewBitmap = new Bitmap(NewWidth, NewHeight);
Graphics g = Graphics.FromImage(NewBitmap);
g.InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor;
g.DrawImage(Yourbitmap, 0, 0, NewWidth, NewHeight);
| | JohnWein Thursday, May 31, 2007 11:01 PM | Resize it with graphics and set the interpolation mode to nearest neighbor. | | JohnWein Tuesday, May 29, 2007 10:34 PM | You mean I should use bitmap object?
Thanks,
aw | | Joan Venge Tuesday, May 29, 2007 10:36 PM |
Dim MyImage As New Bitmap("C:\untitled.bmp")
Dim factor As Double = 0.5
Dim MyThumbNail As Bitmap
MyThumbNail = New Bitmap(MyImage, New Size(MyImage.Size.Width * factor, MyImage.Size.Height * factor))
MyThumbNail .Save( "C:\Thumb.bmp")
But this is even more elegant:
Dim MyImg As New Bitmap("C:\untitled.bmp")
Dim Factor as double = 0.5
Dim MyThumb As Image
MyThumb = MyImg.GetThumbnailImage(MyImg.Size.Width * factor, MyImg.Size.Height * factor, AddressOf ThumbNailAbort, Nothing)
MyThumb.Save( "C:\k.bmp")
Private Function ThumbNailAbort() As Boolean
'Do Nothing Here
End Function
Hope this helps | | Karl Davies-Barrett - MCT Tuesday, May 29, 2007 11:16 PM | Sadly, the Graphics class is missing a "do not interpolate at all" mode. You'll get blending artifacts, no matter which Interpolation mode you try. Here's a solution, it works by resizing with the old GDI StretchBlt() function.
public static Image ResizeImage(Image srce, int width, int height) { Bitmap dest = new Bitmap(srce, width, height); Graphics grDest = Graphics.FromImage(dest); IntPtr hdcDest = grDest.GetHdc(); Graphics grSrce = Graphics.FromImage(srce); IntPtr hdcSrce = grSrce.GetHdc(); StretchBlt(hdcDest, 0, 0, width, height, hdcSrce, 0, 0, srce.Width, srce.Height, SRCCOPY); grSrce.ReleaseHdc(hdcSrce); grDest.ReleaseHdc(hdcDest); grSrce.Dispose(); grDest.Dispose(); return dest; } [System.Runtime.InteropServices.DllImport("gdi32.dll")] static extern bool StretchBlt(IntPtr hdcDest, int nXOriginDest, int nYOriginDest, int nWidthDest, int nHeightDest, IntPtr hdcSrc, int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc, int dwRop); private static int SRCCOPY = 0xCC0020;
| | nobugz Wednesday, May 30, 2007 12:02 AM | I thought you had a Bitmap?
Get the graphics from the Bitmap and set the interpolation mode to nearest neighbor.
Code Snippet
Dim g As Graphics = Graphics.FromImage(YourBitmap)
g.InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor
g.DrawImage(Yourbitmap, 0, 0, NewWidth, NewHeight)
| | JohnWein Wednesday, May 30, 2007 8:38 AM | Hey thanks for the replies.
Karl, I am not sure how to use your method. My bitmap is created only in memory.
John, I tried your method, but then when I set the image of the control, should I use preview or g? I tried g but the compiler said it cannot convert it to bitmap/image.
Nobugz, I tried your method like this:
picHSV.Image = ResizeImage ( preview, 600, 600 ); but it didn't change my last result. Same smoothing artifacts.
Preview is my bitmap.
Is there a way to achieve this? What I am doing wrong?
Thanks again all,
aw | | Joan Venge Thursday, May 31, 2007 7:40 PM | Well, that's pretty odd. The StretchBlt() I knew certainly wasn't smart enough to do any smoothing. Are you sure you're talking about "smoothing" and not "pixellation" effects? Are you growing or shrinking the image? Posting a screenshot at a place like imageshack.com might help.
| | nobugz Thursday, May 31, 2007 7:49 PM | Thanks man. Here is the image: http://aycu32.webshots.com/image/18951/2003344856306196893_rs.jpg
Notice the smoothing around? It should be sharp, because all I do is set a few pixels.
Thanks again,
aw | | Joan Venge Thursday, May 31, 2007 8:11 PM | Ouch, my code doesn't work. It was bound to happen but I'll now have to drop the "no" prefix on my nick. Stupidly, I tested it by shrinking the image, not by growing it. As yet, I have no clue what might be wrong, StretchBlt() returns TRUE to indicate it worked but it doesn't actually copy the bitmap. If anybody sees a mistake, please shout. Using a compatible DC is what I need to try next, I'll need some time for that.
| | nobugz Thursday, May 31, 2007 9:08 PM | Thanks alot I think it's still a great effort.
Have you tried to grow an image to see if it fails? Because I am not sure %100.
Thanks again,
aw | | Joan Venge Thursday, May 31, 2007 9:50 PM | I know I have a problem with communication, but do you or do you not have a Bitmap? There are many ways to create a Bitmap, but once it is created it resides in memory. In my code "YourBitmap" has type Bitmap. When I use NearestNeighbor interpolation, the resultant image doesn't appear tohave anyinterpolation artifacts. "NewWidth" and "NewHeight" are your desired width and height for your resized "Bitmap". | | JohnWein Thursday, May 31, 2007 10:21 PM | http://aycu32.webshots.com/image/18951/2003344856306196893_rs.jpg
Why would you expect it to be sharp if it's a jpeg image? This is why jpeg is not used for line drawings. Use jpeg only for photographs. | | JohnWein Thursday, May 31, 2007 10:31 PM | Thanks man. I have a bitmap. Basically what I do is, set the first ten pixels to different colors. And when you resize it, it should look like you have 10 boxes of colors, and not smeary looking colored image.
So to make sure, you can try doing them same to see if it works?
Thanks again,
aw | | Joan Venge Thursday, May 31, 2007 10:35 PM | Yep, John is right. If you use InterpolationMode.NearestNeighbor and you *grow* the image, it simply makes "fat" pixels without interpolating. My apologies to John and you to make this far harder than it needed to be. Please unmark all answers and give it to John.
| | nobugz Thursday, May 31, 2007 10:41 PM | Yeh. Well my code is wrong it should be:
Code Snippet
Dim NewWidth, NewHeight As Int32
Dim NewBitmap As New Bitmap(NewWidth, NewHeight)
Dim g As Graphics = Graphics.FromImage(NewBitmap)
g.InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor
g.DrawImage(Yourbitmap, 0, 0, NewWidth, NewHeight)
Edit:
Or, using Instant C#
Code Snippet
Int32 NewWidth = 0;
Int32 NewHeight = 0;
Bitmap NewBitmap = new Bitmap(NewWidth, NewHeight);
Graphics g = Graphics.FromImage(NewBitmap);
g.InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor;
g.DrawImage(Yourbitmap, 0, 0, NewWidth, NewHeight);
| | JohnWein Thursday, May 31, 2007 11:01 PM | Thanks guys! I just have one more question:
What should I assign to my control's image property, g or NewBitmap or yourbitmap?
Thanks,
aw | | Joan Venge Friday, June 01, 2007 2:24 PM | You can't successfully assign a Graphics to an Image. Assign whatever Bitmap you want to show to the Image property. | | JohnWein Friday, June 01, 2007 2:41 PM |
|