473,385 Members | 1,983 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Drawing using NearestNeighbor

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
Nov 20 '05 #1
3 1516
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.

Nov 20 '05 #2
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?

-----Original Message-----
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 AsSystem.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 AsSingle) 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 AsSystem.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.
.

Nov 20 '05 #3
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/de...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.

Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Leif K-Brooks | last post by:
I'm considering writing a fairly basic vector drawing program using PyGTK. What's the best way to render it? What I'm thinking of is having everything render on a PIL image, then display that in...
2
by: Champika Nirosh | last post by:
Hi, I want to create drawing board application that can draw Line, rectagle, circle and free hand drawing. Each drawing need to be transparent, moveable (draggable), have bring to front and...
4
by: Stuart Norris | last post by:
Dear Readers, I am attempting to draw box around some text using unicode on multiline label. The label is forty characters wide and 12 lines deep. I have been trying to draw a box around text...
7
by: Marc Pelletier | last post by:
Hello all, I have a class which includes a method to create a chart. I want to be able to call this method from asp.net code as well as windows application code, so I have sketched it out as...
0
by: Hasim AH | last post by:
Hi .. Just getting interested to learn C# and needs help. I want to write C# application so that the program will execute and draw graphics when the user select the drawing menu from the main...
11
by: dongarbage | last post by:
Hi there, I'm very much a C# novice. How do you do freehand drawing on a panel with a mouse in c#? Thanks, Don
1
by: YouPoP | last post by:
I am doing an app (C# 2.0) where you can draw in a panel with your mouse in "real time". I actually have 2 problems; 1- it does not really is "real time", if your mouse move fast or very fast the...
2
by: ThatsIT.net.au | last post by:
I have this code that writes a pie chart in a asp.net page, but I want to use it in a server control. When I try I get a error on the last line "Response.OutputStream" Obviously there is no...
7
by: raylopez99 | last post by:
I have a logical drawing space much bigger than the viewport (the screen) and I'd like to center the viewport (the screen) to be at the center of the logical drawing space. After following the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.