Drawing using NearestNeighbor

  • Thread starter Thread starter Lance
  • Start date Start date
L

Lance

How do you enable
Graphics.InterpolationMode.NearestNeighbor? When I use
the following code it does not work (i.e., the image
appears to have been drawn using linear interpolation):

Const scale As Single = 10
Dim b As New Drawing.TextureBrush(MyBitmap)
Dim g As Drawing.Graphics = MyPictureBox.CreateGraphics
g.InterpolationMode = InterpolationMode.NearestNeighbor
b.ScaleTransform(scale, scale)
'I've also tried to scale g, but that doesn't work either
'i.e., g.ScaleTransform(scale, scale)
g.FillRectangle(b, MyPictureBox.ClientRectangle)

Thanks,
Lance
 
Hi Lance,

I think you may need to scale the image first before your create the brush.

Private bmp As Bitmap
Private b As TextureBrush
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim MyBitmap As New Bitmap("c:\test.bmp")
Const scale As Single = 2
bmp = GetScaledImage(MyBitmap, scale)
b = New Drawing.TextureBrush(bmp)
Me.Size = New Size(MyBitmap.Width * scale, MyBitmap.Height * scale)
End Sub
Private Function GetScaledImage(ByVal srcbmp As Bitmap, ByVal scale As
Single) As Bitmap
Dim bm As New Bitmap(srcbmp.Width * scale, srcbmp.Height * scale,
Imaging.PixelFormat.Format24bppRgb)
Dim g As Graphics
g = Graphics.FromImage(bm)
g.ScaleTransform(scale, scale)
g.InterpolationMode = InterpolationMode.Bilinear
g.DrawImage(srcbmp, 0, 0)
g.Dispose()
Return bm
End Function

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
e.Graphics.FillRectangle(b, New Rectangle(New Point(0, 0),
Me.ClientSize))
End Sub

If you have any concern on this issue, please post here.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Thanks for the help. I think there must be another way
though. Lets say that a user is viewing a 1600x1200 image
and they zoom in on the image to 100x. So now I have to
create a 160000x120000 image in order for the image to be
displayed as NearestNeighbor?
 
Hi Lance,

If you just need to display an image, you may try to use the drawimage
method of Graphics object.

Using Interpolation Mode to Control Image Quality During Scaling
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdicpp/GDIP
lus/UsingGDIPlus/UsingImagesBitmapsandMetafiles/UsingInterpolationModetoCont
rolImageQualityDuringScaling.asp

Based on my research and test on my side, when using the FillRectangle
method, Graphics object will not care the InterpolationMode setting.

Can you tell me why you need to create an textureBrush with a large bitmap,
maybe there will be a better solution?
e.g. you can create the bitmap with less scale and set the Graphics with
more scale.

If you have any concern on this issue, please post here.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top