SlideShare a Scribd company logo
1 of 14
Private Sub btnScale_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnScale.Click
' Get the scale factor.
Dim scale_factor As Single = Single.Parse(txtScale.Text)
' Get the source bitmap.
Dim bm_source As New Bitmap(picSource.Image)
' Make a bitmap for the result.
Dim bm_dest As New Bitmap( _
CInt(bm_source.Width * scale_factor), _
CInt(bm_source.Height * scale_factor))
' Make a Graphics object for the result Bitmap.
Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)
' Copy the source image into the destination bitmap.
gr_dest.DrawImage(bm_source, 0, 0, _
bm_dest.Width + 1, _
bm_dest.Height + 1)
' Display the result.
picDest.Image = bm_dest
End Sub
Public Shared Function ResizeImage(ByVal InputImage As Image) As Image
Return New Bitmap(InputImage, New Size(64, 64))
End Function
Where;
1. "InputImage" is the image you want to resize.
2. "64 X 64" is the required size you may change it as your needs i.e 32X32 etc.
#Region " ResizeImage "
Public Overloads Shared Function ResizeImage(SourceImage As
Drawing.Image, TargetWidth As Int32, TargetHeight As Int32) As Drawing.Bitmap
Dim bmSource = New Drawing.Bitmap(SourceImage)
Return ResizeImage(bmSource, TargetWidth, TargetHeight)
End Function
Public Overloads Shared Function ResizeImage(bmSource As Drawing.Bitmap,
TargetWidth As Int32, TargetHeight As Int32) As Drawing.Bitmap
Dim bmDest As New Drawing.Bitmap(TargetWidth, TargetHeight,
Drawing.Imaging.PixelFormat.Format32bppArgb)
Dim nSourceAspectRatio = bmSource.Width / bmSource.Height
Dim nDestAspectRatio = bmDest.Width / bmDest.Height
Dim NewX = 0
Dim NewY = 0
Dim NewWidth = bmDest.Width
Dim NewHeight = bmDest.Height
If nDestAspectRatio = nSourceAspectRatio Then
'same ratio
ElseIf nDestAspectRatio > nSourceAspectRatio Then
'Source is taller
NewWidth = Convert.ToInt32(Math.Floor(nSourceAspectRatio *
NewHeight))
NewX = Convert.ToInt32(Math.Floor((bmDest.Width - NewWidth) / 2))
Else
'Source is wider
NewHeight = Convert.ToInt32(Math.Floor((1 / nSourceAspectRatio) *
NewWidth))
NewY = Convert.ToInt32(Math.Floor((bmDest.Height - NewHeight) /
2))
End If
Using grDest = Drawing.Graphics.FromImage(bmDest)
With grDest
.CompositingQuality =
Drawing.Drawing2D.CompositingQuality.HighQuality
.InterpolationMode =
Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
.PixelOffsetMode =
Drawing.Drawing2D.PixelOffsetMode.HighQuality
.SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias
.CompositingMode =
Drawing.Drawing2D.CompositingMode.SourceOver
.DrawImage(bmSource, NewX, NewY, NewWidth, NewHeight)
End With
End Using
Return bmDest
End Function
#End Region
Dim source As New Bitmap("C:image.png")
Dim target As New Bitmap(size.Width, size.Height, PixelFormat.Format24bppRgb)
Using graphics As Graphics = Graphics.FromImage(target)
graphics.DrawImage(source, new Size(48, 48))
End Using
Dim x As Integer = 0
Dim y As Integer = 0
Dim k = 0
Dim l = 0
Dim bm As New Bitmap(p1.Image)
Dim om As New Bitmap(p1.Image.Width, p1.Image.Height)
Dim r, g, b As Byte
Do While x < bm.Width - 1
y = 0
l = 0
Do While y < bm.Height - 1
r = 255 - bm.GetPixel(x, y).R
g = 255 - bm.GetPixel(x, y).G
b = 255 - bm.GetPixel(x, y).B
om.SetPixel(k, l, Color.FromArgb(r, g, b))
y += 3
l += 1
Loop
x += 3
k += 1
Loop
p2.Image = om
Private Sub btnScale_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnScale.Click
' Get the scale factor.
Dim scale_factor As Single = Single.Parse(txtScale.Text)
' Get the source bitmap.
Dim bm_source As New Bitmap(picSource.Image)
' Make a bitmap for the result.
Dim bm_dest As New Bitmap( _
CInt(bm_source.Width * scale_factor), _
CInt(bm_source.Height * scale_factor))
' Make a Graphics object for the result Bitmap.
Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)
' Copy the source image into the destination bitmap.
gr_dest.DrawImage(bm_source, 0, 0, _
bm_dest.Width + 1, _
bm_dest.Height + 1)
' Display the result.
picDest.Image = bm_dest
End Sub
Resizing images with Visual Basic (VB6) and
csXImage
Here are some code fragments showing how to use the ActiveX control csXImage to resize
images. The code is written in Visual Basic (VB 6). It is assumed that you have already installed
either the full or trial version of csXImage and have added a control to a form in Visual Basic.
More on getting started with csXImage in VB.
The ResizeImage method
The ResizeImage method will resize the image that is loaded in the control by specifying a new
width and height. If both parameters are given a non zero value, the new image will have exactly
those dimensions. This can lead to the image losing its aspect ratio. Sometimes it is necessary to
change the aspect ratio, for example with a faxed image that has different pixel densities in the X
and Y directions.
The following code produces an image that is 640 x 480 pixels regardless of its original size.
ImageBox1.ResizeImage 640, 480
If either the width or height parameter of the ResizeImage method is zero, the new image will be
resized to the width or height that is specified and will maintain the aspect ratio.
The following code will produce an image that is 200 pixels wide and it will maintain the aspect
ratio. The final height will vary depending on the original height. Note that the new image will
always be 200 pixels wide even if it was smaller to start with.
ImageBox1.ResizeImage 200, 0
The ResizeFit method
The ResizeFit method also takes a width and height parameter but it will always maintain aspect
ratio and it will never enlarge an image. The new image will always fit into the size defined by
the parameters.
The following code will resize the image to fit inside a box measuring 200 pixels wide by 100
pixels high. An image with dimensions 300 x 200 will be resized to 150 x 100 but an image that
is 500 x 200 will be resized to 200 x 80 pixels.
ImageBox1.ResizeFit 200, 100
The ScaleImage method
The ScaleImage method takes a single parameter which is the percentage scaling that will be
applied. 100 would cause no change, 50 would reduce the image to half the width and half the
height. 200 would double the width and height. For example:
ImageBox1.ScaleImage 50
When an image is resized with csXImage, any supported meta data will be preserved, including
IPTC, EXIF and ICC colour profiles. This meta data can be removed if required.
The resized image will appear smoother if it is a 24 bit image. That is, if the ColorFormat
property is set to cf24bit. If the resized image contains jagged edges, the Resample property
could be set before the resize. This gives a smoother image but it can lose detail, especially small
text. When using Resample set the FilterType property as well.
Mengubah ukuran Gambar di VB.NET
Saya ingin membuat utilitas VB sederhana untuk mengubah ukuran gambar menggunakan
vb.net. Saya mengalami kesulitan mencari tahu kelas vb apa yang digunakan untuk benar-benar
memanipulasi gambar. Kelas Gambar dan kelas Bitmap tidak berfungsi.
Setiap ide, petunjuk, kiat, tautan tutorial sangat dihargai.
Terima kasih.
vb.netimageresizeimage-manipulation
12
27 Jan 2010Moshe
Berikut ini adalah artikel dengan rincian lengkap tentang cara melakukan ini.
Private Sub btnScale_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnScale.Click
' Get the scale factor.
Dim scale_factor As Single = Single.Parse(txtScale.Text)
' Get the source bitmap.
Dim bm_source As New Bitmap(picSource.Image)
' Make a bitmap for the result.
Dim bm_dest As New Bitmap( _
CInt(bm_source.Width * scale_factor), _
CInt(bm_source.Height * scale_factor))
' Make a Graphics object for the result Bitmap.
Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)
' Copy the source image into the destination bitmap.
gr_dest.DrawImage(bm_source, 0, 0, _
bm_dest.Width + 1, _
bm_dest.Height + 1)
' Display the result.
picDest.Image = bm_dest
End Sub
[Sunting]
Satu lagi pada baris yang sama.
14
27 Jan 2010
Binoj Antony
The most convenient and reliable file
storage service
Receive your personal cloud storage with 2Gb of space for free
Anda cukup menggunakan kode satu baris ini untuk mengubah ukuran gambar Anda dalam
visual basic .net
Public Shared Function ResizeImage(ByVal InputImage As Image) As Image
Return New Bitmap(InputImage, New Size(64, 64))
End Function
Dimana;
1. "InputImage"adalahgambaryanginginAndaubahukurannya.
2. "64 X 64" adalahukuran yangdiperlukan,Andadapatmengubahnyasesuaikebutuhan,mis.
32X32 dll.
16
18 Mar 2014
MuhammadSaqib
Ini akan mengubah ukuran gambar apa pun menggunakan kualitas terbaik dengan dukungan
untuk 32bpp dengan alpha. Gambar baru akan memiliki gambar asli berpusat di dalam yang baru
dengan rasio aspek asli.
#Region " ResizeImage "
Public Overloads Shared Function ResizeImage(SourceImage As
Drawing.Image, TargetWidth As Int32, TargetHeight As Int32) As Drawing.Bitmap
Dim bmSource = New Drawing.Bitmap(SourceImage)
Return ResizeImage(bmSource, TargetWidth, TargetHeight)
End Function
Public Overloads Shared Function ResizeImage(bmSource As Drawing.Bitmap,
TargetWidth As Int32, TargetHeight As Int32) As Drawing.Bitmap
Dim bmDest As New Drawing.Bitmap(TargetWidth, TargetHeight,
Drawing.Imaging.PixelFormat.Format32bppArgb)
Dim nSourceAspectRatio = bmSource.Width / bmSource.Height
Dim nDestAspectRatio = bmDest.Width / bmDest.Height
Dim NewX = 0
Dim NewY = 0
Dim NewWidth = bmDest.Width
Dim NewHeight = bmDest.Height
If nDestAspectRatio = nSourceAspectRatio Then
'same ratio
ElseIf nDestAspectRatio > nSourceAspectRatio Then
'Source is taller
NewWidth = Convert.ToInt32(Math.Floor(nSourceAspectRatio *
NewHeight))
NewX = Convert.ToInt32(Math.Floor((bmDest.Width - NewWidth) / 2))
Else
'Source is wider
NewHeight = Convert.ToInt32(Math.Floor((1 / nSourceAspectRatio) *
NewWidth))
NewY = Convert.ToInt32(Math.Floor((bmDest.Height - NewHeight) /
2))
End If
Using grDest = Drawing.Graphics.FromImage(bmDest)
With grDest
.CompositingQuality =
Drawing.Drawing2D.CompositingQuality.HighQuality
.InterpolationMode =
Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
.PixelOffsetMode =
Drawing.Drawing2D.PixelOffsetMode.HighQuality
.SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias
.CompositingMode =
Drawing.Drawing2D.CompositingMode.SourceOver
.DrawImage(bmSource, NewX, NewY, NewWidth, NewHeight)
End With
End Using
Return bmDest
End Function
#End Region
4
11 Mar 2013
Carter Medlin
Tidak tahu banyak sintaks VB.NET tapi di sini dan idenya
Dim source As New Bitmap("C:image.png")
Dim target As New Bitmap(size.Width, size.Height, PixelFormat.Format24bppRgb)
Using graphics As Graphics = Graphics.FromImage(target)
graphics.DrawImage(source, new Size(48, 48))
End Using
2
27 Jan 2010
Alex LE
Dim x As Integer = 0
Dim y As Integer = 0
Dim k = 0
Dim l = 0
Dim bm As New Bitmap(p1.Image)
Dim om As New Bitmap(p1.Image.Width, p1.Image.Height)
Dim r, g, b As Byte
Do While x < bm.Width - 1
y = 0
l = 0
Do While y < bm.Height - 1
r = 255 - bm.GetPixel(x, y).R
g = 255 - bm.GetPixel(x, y).G
b = 255 - bm.GetPixel(x, y).B
om.SetPixel(k, l, Color.FromArgb(r, g, b))
y += 3
l += 1
Loop
x += 3
k += 1
Loop
p2.Image = om
Public Sub AutosizeImage(ByVal ImagePath As String, ByVal picBox As
PictureBox, Optional ByVal pSizeMode As PictureBoxSizeMode =
PictureBoxSizeMode.CenterImage)
Try
picBox.Image = Nothing
picBox.SizeMode = pSizeMode
If System.IO.File.Exists(ImagePath) Then
Dim imgOrg As Bitmap
Dim imgShow As Bitmap
Dim g As Graphics
Dim divideBy, divideByH, divideByW As Double
imgOrg = DirectCast(Bitmap.FromFile(ImagePath), Bitmap)
divideByW = imgOrg.Width / picBox.Width
divideByH = imgOrg.Height / picBox.Height
If divideByW > 1 Or divideByH > 1 Then
If divideByW > divideByH Then
divideBy = divideByW
Else
divideBy = divideByH
End If
imgShow = New Bitmap(CInt(CDbl(imgOrg.Width) / divideBy),
CInt(CDbl(imgOrg.Height) / divideBy))
imgShow.SetResolution(imgOrg.HorizontalResolution,
imgOrg.VerticalResolution)
g = Graphics.FromImage(imgShow)
g.InterpolationMode =
Drawing2D.InterpolationMode.HighQualityBicubic
g.DrawImage(imgOrg, New Rectangle(0, 0,
CInt(CDbl(imgOrg.Width) / divideBy), CInt(CDbl(imgOrg.Height) / divideBy)),
0, 0, imgOrg.Width, imgOrg.Height, GraphicsUnit.Pixel)
g.Dispose()
Else
imgShow = New Bitmap(imgOrg.Width, imgOrg.Height)
imgShow.SetResolution(imgOrg.HorizontalResolution,
imgOrg.VerticalResolution)
g = Graphics.FromImage(imgShow)
g.InterpolationMode =
Drawing2D.InterpolationMode.HighQualityBicubic
g.DrawImage(imgOrg, New Rectangle(0, 0, imgOrg.Width,
imgOrg.Height), 0, 0, imgOrg.Width, imgOrg.Height, GraphicsUnit.Pixel)
g.Dispose()
End If
imgOrg.Dispose()
picBox.Image = imgShow
Else
picBox.Image = Nothing
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
This chapter is from the book
Visual BasicProgrammer'sGuide tothe .NET FrameworkClassLibrary
Learn More Buy
This chapter is from the book
Visual BasicProgrammer'sGuide tothe .NET FrameworkClassLibrary
Learn More Buy
Working with Images
In this section, you will see how to use two of the most common graphic types a programmer
interacts with: bitmaps and icons. As stated earlier in the chapter, all user-interface objects in
Windows are some form of a bitmap; a bitmap is simply a collection of pixels set to various
colors.
Images
The namespace library gives us three classes for working with images: Image, Bitmap and Icon.
Image is simply the base class from which the others inherit. Bitmap allows us to convert a
graphics file into the native GDI+ format (bitmap). This class can be used to define images as fill
patterns, transform images for display, define the look of a button—its uses are many. Although
the bitmap format is used to manipulate images at the pixel level, GDI+ can actually work with
the following image types:
 Bitmaps (BMP)
 Graphics Interchange Format (GIF)
 Joint Photographic Experts Group (JPEG)
 Exchangeable Image File (EXIF)
 Portable Network Graphics (PNG)
 Tag Image File Format (TIFF)
Creating an instance of Bitmap requires a filename, stream, or another valid Image instance. For
example, the following line of code will instantiate a Bitmap object based on a JPEG file:
Dim myBitmap As New System.Drawing.Bitmap(fileName:="Sample.jpg")
Once instantiated, we can do a number of things with the image. For instance, we can change its
resolution with the SetResolution method or make part of the image transparent with
MakeTransparent. Of course, we will also want to draw our image to the form. We use the
DrawImage method of the Graphics class to output the image to the screen. The DrawImage
method has over 30 overloaded parameter sets. In its simplest form, we pass the method an
instance of Bitmap and the upper-left coordinate of where we want the method to begin drawing.
For example:
myGraphics.DrawImage(image:=myBitmap, point:=New Point(x:=5, y:=5))
Scaling and Cropping
It is often helpful to be able to scale or crop an image to a different size. Suppose you need a 100
x 100 image to fit in a 20 x 20 space, or you want to give your users the ability to zoom in on a
portion of an image. You use a variation of the DrawImage method to scale images. This
overloaded method takes a Rectangle instance as the destination for drawing your image.
However, if the rectangle is smaller or larger than your image, the method will automatically
scale the image to match the bounds of the rectangle.
Another version of the DrawImage method takes both a source rectangle and a destination
rectangle. The source rectangle defines the portion of the original image to be drawn into the
destination rectangle. This, effectively, is cropping. The source rectangle defines how the image
gets cropped when applied to the destination. Of course, you can crop to the original size or scale
the cropped portion to a new size. Listing 9.8 provides a detailed code example of both scaling
and cropping an image.
Listing 9.8 Scale and Crop
Protected Overrides Sub OnClick(ByVal e As System.EventArgs)
'local scope
Dim myBitmap As System.Drawing.Bitmap
Dim myGraphics As Graphics
Dim mySource As Rectangle
Dim myDestination As Rectangle
'create an instance of bitmap based on a file
myBitmap = New System.Drawing.Bitmap(fileName:="dotnet.gif")
'return the current form as a drawing surface
myGraphics = Graphics.FromHwnd(ActiveForm().Handle)
'define a rectangle as the size of the original image (source)
mySource = New Rectangle(x:=0, y:=0, Width:=81, Height:=45)
'draw the original bitmap to the source rectangle
myGraphics.DrawImage(image:=myBitmap, rect:=mySource)
'create a destination rectangle
myDestination = New Rectangle(x:=90, y:=0, Width:=162, Height:=90)
'output the image to the dest. rectangle (scale)
myGraphics.DrawImage(image:=myBitmap, rect:=myDestination)
'output a cropped portion of the source
myGraphics.DrawImage(image:=myBitmap, _
destRect:=New Rectangle(x:=0, y:=100, Width:=30, Height:=30), _
srcRect:=New Rectangle(x:=0, y:=35, Width:=14, Height:=14), _
srcUnit:=GraphicsUnit.Pixel)
End Sub
Notice that we actually drew the image to the form three times. The first time, we drew the
image into a rectangle (mySource) based on its original size. The second time, we scaled the
image to two times its original size (myDestination) by creating a larger rectangle and
outputting the image accordingly. Finally, we cropped a portion of the original output and put it
in a new, larger rectangle. Figure 9.5 shows the code's output to the form.
Figure 9.5 Scale and crop output.
Icons
An icon in Windows is a small bitmap image that represents an object. You cannot go far in
without seeing and working with icons. For example, the File Explorer uses icons to represent
folders and files; your desktop contains icons for My Computer, Recycle Bin, and My Network
Places.
We use the Icon class to work with icons in .NET. We can instantiate an Icon instance in much
the same way we created Bitmap objects. The following code creates an icon based on a file
name:
Dim myIcon as New Icon(fileName:="myIcon.ico")
The DrawIcon method of the Graphics class is used to draw the icon to the form. To it, you can
pass the icon and either just the upper-left x and y coordinates or a bounding rectangle. If you
pass a Rectangle instance, the icon will be scaled based on the bounding rectangle. The
following line of code draws an icon object into a bounding rectangle.
myGraphics.DrawIcon(icon:=myIcon, _
rectangle:=New Rectangle(x:=5, y:=5, width:=32, height:=32))
The Graphics class also gives us the DrawIconUnstretched method that allows us to specify a
bounding rectangle without actually scaling the icon. In fact, if the icon is larger than the
bounding rectangle, it will be cropped to fit from the left corner down and to the right.
Suggestions for Further Exploration
 To animate images, take a look at the ImageAnimator class.
 Check out the SmoothingMode property of the Graphics class and the SmoothingMode
enumeration members. This method allows you to set things like antialiasing to make
your graphics look "smoother."
Resize image vb.net

More Related Content

What's hot

Digital image processing
Digital image processingDigital image processing
Digital image processingtushar05
 
Digital image processing
Digital image processingDigital image processing
Digital image processingDeevena Dayaal
 
Screen and image resolution
Screen and image resolutionScreen and image resolution
Screen and image resolutionlenance
 
Presentation on Digital Image Processing
Presentation on Digital Image ProcessingPresentation on Digital Image Processing
Presentation on Digital Image ProcessingSalim Hosen
 
Face detection ppt
Face detection pptFace detection ppt
Face detection pptPooja R
 
CONTENT BASED IMAGE RETRIEVAL SYSTEM
CONTENT BASED IMAGE RETRIEVAL SYSTEMCONTENT BASED IMAGE RETRIEVAL SYSTEM
CONTENT BASED IMAGE RETRIEVAL SYSTEMVamsi IV
 
Image processing, Noise, Noise Removal filters
Image processing, Noise, Noise Removal filtersImage processing, Noise, Noise Removal filters
Image processing, Noise, Noise Removal filtersKuppusamy P
 

What's hot (9)

Digital image processing
Digital image processingDigital image processing
Digital image processing
 
Digital image processing
Digital image processingDigital image processing
Digital image processing
 
Screen and image resolution
Screen and image resolutionScreen and image resolution
Screen and image resolution
 
Presentation on Digital Image Processing
Presentation on Digital Image ProcessingPresentation on Digital Image Processing
Presentation on Digital Image Processing
 
Face detection ppt
Face detection pptFace detection ppt
Face detection ppt
 
CONTENT BASED IMAGE RETRIEVAL SYSTEM
CONTENT BASED IMAGE RETRIEVAL SYSTEMCONTENT BASED IMAGE RETRIEVAL SYSTEM
CONTENT BASED IMAGE RETRIEVAL SYSTEM
 
Segmentation Techniques -II
Segmentation Techniques -IISegmentation Techniques -II
Segmentation Techniques -II
 
Chap 6 histogram dan operasi dasar
Chap 6 histogram dan operasi dasarChap 6 histogram dan operasi dasar
Chap 6 histogram dan operasi dasar
 
Image processing, Noise, Noise Removal filters
Image processing, Noise, Noise Removal filtersImage processing, Noise, Noise Removal filters
Image processing, Noise, Noise Removal filters
 

Similar to Resize image vb.net

Image_Processing_LECTURE_c#_programming.ppt
Image_Processing_LECTURE_c#_programming.pptImage_Processing_LECTURE_c#_programming.ppt
Image_Processing_LECTURE_c#_programming.pptLOUISSEVERINOROMANO
 
3 track kinect@Bicocca - sdk e camere
3   track kinect@Bicocca - sdk e camere3   track kinect@Bicocca - sdk e camere
3 track kinect@Bicocca - sdk e camereMatteo Valoriani
 
Dital Image Processing (Lab 2+3+4)
Dital Image Processing (Lab 2+3+4)Dital Image Processing (Lab 2+3+4)
Dital Image Processing (Lab 2+3+4)Moe Moe Myint
 
Fundamentals Image and Graphics
Fundamentals Image and GraphicsFundamentals Image and Graphics
Fundamentals Image and GraphicsShrawan Adhikari
 
The code in image3.cpp has the error as shown above, could you help .pdf
The code in image3.cpp has the error as shown above, could you help .pdfThe code in image3.cpp has the error as shown above, could you help .pdf
The code in image3.cpp has the error as shown above, could you help .pdffatoryoutlets
 
Zooming an image in visual basic
Zooming an image in visual basicZooming an image in visual basic
Zooming an image in visual basicHotland Sitorus
 
We are restricted from importing cv2 numpy stats and other.pdf
We are restricted from importing cv2 numpy stats and other.pdfWe are restricted from importing cv2 numpy stats and other.pdf
We are restricted from importing cv2 numpy stats and other.pdfDARSHANACHARYA13
 
Image processing basics using matlab
Image processing basics using matlabImage processing basics using matlab
Image processing basics using matlabAnkur Tyagi
 
Python media library
Python media libraryPython media library
Python media libraryRaginiJain21
 
On the tomcat drive in folder cosc210 you will find file named Paint.docx
On the tomcat drive in folder cosc210 you will find file named Paint.docxOn the tomcat drive in folder cosc210 you will find file named Paint.docx
On the tomcat drive in folder cosc210 you will find file named Paint.docxdunhamadell
 
asmt7~$sc_210_-_assignment_7_fall_15.docasmt7cosc_210_-_as.docx
asmt7~$sc_210_-_assignment_7_fall_15.docasmt7cosc_210_-_as.docxasmt7~$sc_210_-_assignment_7_fall_15.docasmt7cosc_210_-_as.docx
asmt7~$sc_210_-_assignment_7_fall_15.docasmt7cosc_210_-_as.docxfredharris32
 
JonathanWestlake_ComputerVision_Project1
JonathanWestlake_ComputerVision_Project1JonathanWestlake_ComputerVision_Project1
JonathanWestlake_ComputerVision_Project1Jonathan Westlake
 
Core Image: The Most Fun API You're Not Using, CocoaConf Atlanta, December 2014
Core Image: The Most Fun API You're Not Using, CocoaConf Atlanta, December 2014Core Image: The Most Fun API You're Not Using, CocoaConf Atlanta, December 2014
Core Image: The Most Fun API You're Not Using, CocoaConf Atlanta, December 2014Chris Adamson
 
DIP-Enhancement-Spatial.pptx
DIP-Enhancement-Spatial.pptxDIP-Enhancement-Spatial.pptx
DIP-Enhancement-Spatial.pptxNidhiSharma764884
 

Similar to Resize image vb.net (20)

Image_Processing_LECTURE_c#_programming.ppt
Image_Processing_LECTURE_c#_programming.pptImage_Processing_LECTURE_c#_programming.ppt
Image_Processing_LECTURE_c#_programming.ppt
 
3 track kinect@Bicocca - sdk e camere
3   track kinect@Bicocca - sdk e camere3   track kinect@Bicocca - sdk e camere
3 track kinect@Bicocca - sdk e camere
 
Dital Image Processing (Lab 2+3+4)
Dital Image Processing (Lab 2+3+4)Dital Image Processing (Lab 2+3+4)
Dital Image Processing (Lab 2+3+4)
 
M14 overview
M14 overviewM14 overview
M14 overview
 
Fundamentals Image and Graphics
Fundamentals Image and GraphicsFundamentals Image and Graphics
Fundamentals Image and Graphics
 
The code in image3.cpp has the error as shown above, could you help .pdf
The code in image3.cpp has the error as shown above, could you help .pdfThe code in image3.cpp has the error as shown above, could you help .pdf
The code in image3.cpp has the error as shown above, could you help .pdf
 
Zooming an image in visual basic
Zooming an image in visual basicZooming an image in visual basic
Zooming an image in visual basic
 
Dip 2
Dip 2Dip 2
Dip 2
 
We are restricted from importing cv2 numpy stats and other.pdf
We are restricted from importing cv2 numpy stats and other.pdfWe are restricted from importing cv2 numpy stats and other.pdf
We are restricted from importing cv2 numpy stats and other.pdf
 
Image processing basics using matlab
Image processing basics using matlabImage processing basics using matlab
Image processing basics using matlab
 
HTML 5_Canvas
HTML 5_CanvasHTML 5_Canvas
HTML 5_Canvas
 
Topic 1_PPT.pptx
Topic 1_PPT.pptxTopic 1_PPT.pptx
Topic 1_PPT.pptx
 
Dip syntax 4
Dip syntax 4Dip syntax 4
Dip syntax 4
 
Python media library
Python media libraryPython media library
Python media library
 
On the tomcat drive in folder cosc210 you will find file named Paint.docx
On the tomcat drive in folder cosc210 you will find file named Paint.docxOn the tomcat drive in folder cosc210 you will find file named Paint.docx
On the tomcat drive in folder cosc210 you will find file named Paint.docx
 
Bitmap management like a boss
Bitmap management like a bossBitmap management like a boss
Bitmap management like a boss
 
asmt7~$sc_210_-_assignment_7_fall_15.docasmt7cosc_210_-_as.docx
asmt7~$sc_210_-_assignment_7_fall_15.docasmt7cosc_210_-_as.docxasmt7~$sc_210_-_assignment_7_fall_15.docasmt7cosc_210_-_as.docx
asmt7~$sc_210_-_assignment_7_fall_15.docasmt7cosc_210_-_as.docx
 
JonathanWestlake_ComputerVision_Project1
JonathanWestlake_ComputerVision_Project1JonathanWestlake_ComputerVision_Project1
JonathanWestlake_ComputerVision_Project1
 
Core Image: The Most Fun API You're Not Using, CocoaConf Atlanta, December 2014
Core Image: The Most Fun API You're Not Using, CocoaConf Atlanta, December 2014Core Image: The Most Fun API You're Not Using, CocoaConf Atlanta, December 2014
Core Image: The Most Fun API You're Not Using, CocoaConf Atlanta, December 2014
 
DIP-Enhancement-Spatial.pptx
DIP-Enhancement-Spatial.pptxDIP-Enhancement-Spatial.pptx
DIP-Enhancement-Spatial.pptx
 

Recently uploaded

Pests of Blackgram, greengram, cowpea_Dr.UPR.pdf
Pests of Blackgram, greengram, cowpea_Dr.UPR.pdfPests of Blackgram, greengram, cowpea_Dr.UPR.pdf
Pests of Blackgram, greengram, cowpea_Dr.UPR.pdfPirithiRaju
 
GenBio2 - Lesson 1 - Introduction to Genetics.pptx
GenBio2 - Lesson 1 - Introduction to Genetics.pptxGenBio2 - Lesson 1 - Introduction to Genetics.pptx
GenBio2 - Lesson 1 - Introduction to Genetics.pptxBerniceCayabyab1
 
Sulphur & Phosphrus Cycle PowerPoint Presentation (2) [Autosaved]-3-1.pptx
Sulphur & Phosphrus Cycle PowerPoint Presentation (2) [Autosaved]-3-1.pptxSulphur & Phosphrus Cycle PowerPoint Presentation (2) [Autosaved]-3-1.pptx
Sulphur & Phosphrus Cycle PowerPoint Presentation (2) [Autosaved]-3-1.pptxnoordubaliya2003
 
Pests of jatropha_Bionomics_identification_Dr.UPR.pdf
Pests of jatropha_Bionomics_identification_Dr.UPR.pdfPests of jatropha_Bionomics_identification_Dr.UPR.pdf
Pests of jatropha_Bionomics_identification_Dr.UPR.pdfPirithiRaju
 
BUMI DAN ANTARIKSA PROJEK IPAS SMK KELAS X.pdf
BUMI DAN ANTARIKSA PROJEK IPAS SMK KELAS X.pdfBUMI DAN ANTARIKSA PROJEK IPAS SMK KELAS X.pdf
BUMI DAN ANTARIKSA PROJEK IPAS SMK KELAS X.pdfWildaNurAmalia2
 
The dark energy paradox leads to a new structure of spacetime.pptx
The dark energy paradox leads to a new structure of spacetime.pptxThe dark energy paradox leads to a new structure of spacetime.pptx
The dark energy paradox leads to a new structure of spacetime.pptxEran Akiva Sinbar
 
Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
BIOETHICS IN RECOMBINANT DNA TECHNOLOGY.
BIOETHICS IN RECOMBINANT DNA TECHNOLOGY.BIOETHICS IN RECOMBINANT DNA TECHNOLOGY.
BIOETHICS IN RECOMBINANT DNA TECHNOLOGY.PraveenaKalaiselvan1
 
Harmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms PresentationHarmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms Presentationtahreemzahra82
 
Scheme-of-Work-Science-Stage-4 cambridge science.docx
Scheme-of-Work-Science-Stage-4 cambridge science.docxScheme-of-Work-Science-Stage-4 cambridge science.docx
Scheme-of-Work-Science-Stage-4 cambridge science.docxyaramohamed343013
 
Neurodevelopmental disorders according to the dsm 5 tr
Neurodevelopmental disorders according to the dsm 5 trNeurodevelopmental disorders according to the dsm 5 tr
Neurodevelopmental disorders according to the dsm 5 trssuser06f238
 
STOPPED FLOW METHOD & APPLICATION MURUGAVENI B.pptx
STOPPED FLOW METHOD & APPLICATION MURUGAVENI B.pptxSTOPPED FLOW METHOD & APPLICATION MURUGAVENI B.pptx
STOPPED FLOW METHOD & APPLICATION MURUGAVENI B.pptxMurugaveni B
 
Topic 9- General Principles of International Law.pptx
Topic 9- General Principles of International Law.pptxTopic 9- General Principles of International Law.pptx
Topic 9- General Principles of International Law.pptxJorenAcuavera1
 
Behavioral Disorder: Schizophrenia & it's Case Study.pdf
Behavioral Disorder: Schizophrenia & it's Case Study.pdfBehavioral Disorder: Schizophrenia & it's Case Study.pdf
Behavioral Disorder: Schizophrenia & it's Case Study.pdfSELF-EXPLANATORY
 
Base editing, prime editing, Cas13 & RNA editing and organelle base editing
Base editing, prime editing, Cas13 & RNA editing and organelle base editingBase editing, prime editing, Cas13 & RNA editing and organelle base editing
Base editing, prime editing, Cas13 & RNA editing and organelle base editingNetHelix
 
Citronella presentation SlideShare mani upadhyay
Citronella presentation SlideShare mani upadhyayCitronella presentation SlideShare mani upadhyay
Citronella presentation SlideShare mani upadhyayupadhyaymani499
 
Analytical Profile of Coleus Forskohlii | Forskolin .pptx
Analytical Profile of Coleus Forskohlii | Forskolin .pptxAnalytical Profile of Coleus Forskohlii | Forskolin .pptx
Analytical Profile of Coleus Forskohlii | Forskolin .pptxSwapnil Therkar
 
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptx
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptxLIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptx
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptxmalonesandreagweneth
 
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptx
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptxTHE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptx
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptxNandakishor Bhaurao Deshmukh
 

Recently uploaded (20)

Pests of Blackgram, greengram, cowpea_Dr.UPR.pdf
Pests of Blackgram, greengram, cowpea_Dr.UPR.pdfPests of Blackgram, greengram, cowpea_Dr.UPR.pdf
Pests of Blackgram, greengram, cowpea_Dr.UPR.pdf
 
GenBio2 - Lesson 1 - Introduction to Genetics.pptx
GenBio2 - Lesson 1 - Introduction to Genetics.pptxGenBio2 - Lesson 1 - Introduction to Genetics.pptx
GenBio2 - Lesson 1 - Introduction to Genetics.pptx
 
Sulphur & Phosphrus Cycle PowerPoint Presentation (2) [Autosaved]-3-1.pptx
Sulphur & Phosphrus Cycle PowerPoint Presentation (2) [Autosaved]-3-1.pptxSulphur & Phosphrus Cycle PowerPoint Presentation (2) [Autosaved]-3-1.pptx
Sulphur & Phosphrus Cycle PowerPoint Presentation (2) [Autosaved]-3-1.pptx
 
Pests of jatropha_Bionomics_identification_Dr.UPR.pdf
Pests of jatropha_Bionomics_identification_Dr.UPR.pdfPests of jatropha_Bionomics_identification_Dr.UPR.pdf
Pests of jatropha_Bionomics_identification_Dr.UPR.pdf
 
BUMI DAN ANTARIKSA PROJEK IPAS SMK KELAS X.pdf
BUMI DAN ANTARIKSA PROJEK IPAS SMK KELAS X.pdfBUMI DAN ANTARIKSA PROJEK IPAS SMK KELAS X.pdf
BUMI DAN ANTARIKSA PROJEK IPAS SMK KELAS X.pdf
 
The dark energy paradox leads to a new structure of spacetime.pptx
The dark energy paradox leads to a new structure of spacetime.pptxThe dark energy paradox leads to a new structure of spacetime.pptx
The dark energy paradox leads to a new structure of spacetime.pptx
 
Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝
 
BIOETHICS IN RECOMBINANT DNA TECHNOLOGY.
BIOETHICS IN RECOMBINANT DNA TECHNOLOGY.BIOETHICS IN RECOMBINANT DNA TECHNOLOGY.
BIOETHICS IN RECOMBINANT DNA TECHNOLOGY.
 
Harmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms PresentationHarmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms Presentation
 
Hot Sexy call girls in Moti Nagar,🔝 9953056974 🔝 escort Service
Hot Sexy call girls in  Moti Nagar,🔝 9953056974 🔝 escort ServiceHot Sexy call girls in  Moti Nagar,🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Moti Nagar,🔝 9953056974 🔝 escort Service
 
Scheme-of-Work-Science-Stage-4 cambridge science.docx
Scheme-of-Work-Science-Stage-4 cambridge science.docxScheme-of-Work-Science-Stage-4 cambridge science.docx
Scheme-of-Work-Science-Stage-4 cambridge science.docx
 
Neurodevelopmental disorders according to the dsm 5 tr
Neurodevelopmental disorders according to the dsm 5 trNeurodevelopmental disorders according to the dsm 5 tr
Neurodevelopmental disorders according to the dsm 5 tr
 
STOPPED FLOW METHOD & APPLICATION MURUGAVENI B.pptx
STOPPED FLOW METHOD & APPLICATION MURUGAVENI B.pptxSTOPPED FLOW METHOD & APPLICATION MURUGAVENI B.pptx
STOPPED FLOW METHOD & APPLICATION MURUGAVENI B.pptx
 
Topic 9- General Principles of International Law.pptx
Topic 9- General Principles of International Law.pptxTopic 9- General Principles of International Law.pptx
Topic 9- General Principles of International Law.pptx
 
Behavioral Disorder: Schizophrenia & it's Case Study.pdf
Behavioral Disorder: Schizophrenia & it's Case Study.pdfBehavioral Disorder: Schizophrenia & it's Case Study.pdf
Behavioral Disorder: Schizophrenia & it's Case Study.pdf
 
Base editing, prime editing, Cas13 & RNA editing and organelle base editing
Base editing, prime editing, Cas13 & RNA editing and organelle base editingBase editing, prime editing, Cas13 & RNA editing and organelle base editing
Base editing, prime editing, Cas13 & RNA editing and organelle base editing
 
Citronella presentation SlideShare mani upadhyay
Citronella presentation SlideShare mani upadhyayCitronella presentation SlideShare mani upadhyay
Citronella presentation SlideShare mani upadhyay
 
Analytical Profile of Coleus Forskohlii | Forskolin .pptx
Analytical Profile of Coleus Forskohlii | Forskolin .pptxAnalytical Profile of Coleus Forskohlii | Forskolin .pptx
Analytical Profile of Coleus Forskohlii | Forskolin .pptx
 
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptx
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptxLIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptx
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptx
 
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptx
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptxTHE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptx
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptx
 

Resize image vb.net

  • 1. Private Sub btnScale_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnScale.Click ' Get the scale factor. Dim scale_factor As Single = Single.Parse(txtScale.Text) ' Get the source bitmap. Dim bm_source As New Bitmap(picSource.Image) ' Make a bitmap for the result. Dim bm_dest As New Bitmap( _ CInt(bm_source.Width * scale_factor), _ CInt(bm_source.Height * scale_factor)) ' Make a Graphics object for the result Bitmap. Dim gr_dest As Graphics = Graphics.FromImage(bm_dest) ' Copy the source image into the destination bitmap. gr_dest.DrawImage(bm_source, 0, 0, _ bm_dest.Width + 1, _ bm_dest.Height + 1) ' Display the result. picDest.Image = bm_dest End Sub Public Shared Function ResizeImage(ByVal InputImage As Image) As Image Return New Bitmap(InputImage, New Size(64, 64)) End Function Where; 1. "InputImage" is the image you want to resize. 2. "64 X 64" is the required size you may change it as your needs i.e 32X32 etc. #Region " ResizeImage "
  • 2. Public Overloads Shared Function ResizeImage(SourceImage As Drawing.Image, TargetWidth As Int32, TargetHeight As Int32) As Drawing.Bitmap Dim bmSource = New Drawing.Bitmap(SourceImage) Return ResizeImage(bmSource, TargetWidth, TargetHeight) End Function Public Overloads Shared Function ResizeImage(bmSource As Drawing.Bitmap, TargetWidth As Int32, TargetHeight As Int32) As Drawing.Bitmap Dim bmDest As New Drawing.Bitmap(TargetWidth, TargetHeight, Drawing.Imaging.PixelFormat.Format32bppArgb) Dim nSourceAspectRatio = bmSource.Width / bmSource.Height Dim nDestAspectRatio = bmDest.Width / bmDest.Height Dim NewX = 0 Dim NewY = 0 Dim NewWidth = bmDest.Width Dim NewHeight = bmDest.Height If nDestAspectRatio = nSourceAspectRatio Then 'same ratio ElseIf nDestAspectRatio > nSourceAspectRatio Then 'Source is taller NewWidth = Convert.ToInt32(Math.Floor(nSourceAspectRatio * NewHeight)) NewX = Convert.ToInt32(Math.Floor((bmDest.Width - NewWidth) / 2)) Else 'Source is wider NewHeight = Convert.ToInt32(Math.Floor((1 / nSourceAspectRatio) * NewWidth)) NewY = Convert.ToInt32(Math.Floor((bmDest.Height - NewHeight) / 2)) End If Using grDest = Drawing.Graphics.FromImage(bmDest) With grDest .CompositingQuality = Drawing.Drawing2D.CompositingQuality.HighQuality .InterpolationMode = Drawing.Drawing2D.InterpolationMode.HighQualityBicubic .PixelOffsetMode = Drawing.Drawing2D.PixelOffsetMode.HighQuality .SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias .CompositingMode = Drawing.Drawing2D.CompositingMode.SourceOver .DrawImage(bmSource, NewX, NewY, NewWidth, NewHeight) End With End Using Return bmDest End Function #End Region
  • 3. Dim source As New Bitmap("C:image.png") Dim target As New Bitmap(size.Width, size.Height, PixelFormat.Format24bppRgb) Using graphics As Graphics = Graphics.FromImage(target) graphics.DrawImage(source, new Size(48, 48)) End Using Dim x As Integer = 0 Dim y As Integer = 0 Dim k = 0 Dim l = 0 Dim bm As New Bitmap(p1.Image) Dim om As New Bitmap(p1.Image.Width, p1.Image.Height) Dim r, g, b As Byte Do While x < bm.Width - 1 y = 0 l = 0 Do While y < bm.Height - 1 r = 255 - bm.GetPixel(x, y).R g = 255 - bm.GetPixel(x, y).G b = 255 - bm.GetPixel(x, y).B om.SetPixel(k, l, Color.FromArgb(r, g, b)) y += 3 l += 1 Loop x += 3 k += 1 Loop p2.Image = om
  • 4. Private Sub btnScale_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnScale.Click ' Get the scale factor. Dim scale_factor As Single = Single.Parse(txtScale.Text) ' Get the source bitmap. Dim bm_source As New Bitmap(picSource.Image) ' Make a bitmap for the result. Dim bm_dest As New Bitmap( _ CInt(bm_source.Width * scale_factor), _ CInt(bm_source.Height * scale_factor)) ' Make a Graphics object for the result Bitmap. Dim gr_dest As Graphics = Graphics.FromImage(bm_dest) ' Copy the source image into the destination bitmap. gr_dest.DrawImage(bm_source, 0, 0, _ bm_dest.Width + 1, _ bm_dest.Height + 1) ' Display the result. picDest.Image = bm_dest End Sub Resizing images with Visual Basic (VB6) and csXImage Here are some code fragments showing how to use the ActiveX control csXImage to resize images. The code is written in Visual Basic (VB 6). It is assumed that you have already installed either the full or trial version of csXImage and have added a control to a form in Visual Basic. More on getting started with csXImage in VB. The ResizeImage method The ResizeImage method will resize the image that is loaded in the control by specifying a new width and height. If both parameters are given a non zero value, the new image will have exactly those dimensions. This can lead to the image losing its aspect ratio. Sometimes it is necessary to change the aspect ratio, for example with a faxed image that has different pixel densities in the X and Y directions. The following code produces an image that is 640 x 480 pixels regardless of its original size. ImageBox1.ResizeImage 640, 480
  • 5. If either the width or height parameter of the ResizeImage method is zero, the new image will be resized to the width or height that is specified and will maintain the aspect ratio. The following code will produce an image that is 200 pixels wide and it will maintain the aspect ratio. The final height will vary depending on the original height. Note that the new image will always be 200 pixels wide even if it was smaller to start with. ImageBox1.ResizeImage 200, 0 The ResizeFit method The ResizeFit method also takes a width and height parameter but it will always maintain aspect ratio and it will never enlarge an image. The new image will always fit into the size defined by the parameters. The following code will resize the image to fit inside a box measuring 200 pixels wide by 100 pixels high. An image with dimensions 300 x 200 will be resized to 150 x 100 but an image that is 500 x 200 will be resized to 200 x 80 pixels. ImageBox1.ResizeFit 200, 100 The ScaleImage method The ScaleImage method takes a single parameter which is the percentage scaling that will be applied. 100 would cause no change, 50 would reduce the image to half the width and half the height. 200 would double the width and height. For example: ImageBox1.ScaleImage 50 When an image is resized with csXImage, any supported meta data will be preserved, including IPTC, EXIF and ICC colour profiles. This meta data can be removed if required. The resized image will appear smoother if it is a 24 bit image. That is, if the ColorFormat property is set to cf24bit. If the resized image contains jagged edges, the Resample property could be set before the resize. This gives a smoother image but it can lose detail, especially small text. When using Resample set the FilterType property as well.
  • 6. Mengubah ukuran Gambar di VB.NET Saya ingin membuat utilitas VB sederhana untuk mengubah ukuran gambar menggunakan vb.net. Saya mengalami kesulitan mencari tahu kelas vb apa yang digunakan untuk benar-benar memanipulasi gambar. Kelas Gambar dan kelas Bitmap tidak berfungsi. Setiap ide, petunjuk, kiat, tautan tutorial sangat dihargai. Terima kasih. vb.netimageresizeimage-manipulation 12 27 Jan 2010Moshe Berikut ini adalah artikel dengan rincian lengkap tentang cara melakukan ini. Private Sub btnScale_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnScale.Click ' Get the scale factor. Dim scale_factor As Single = Single.Parse(txtScale.Text) ' Get the source bitmap. Dim bm_source As New Bitmap(picSource.Image) ' Make a bitmap for the result. Dim bm_dest As New Bitmap( _ CInt(bm_source.Width * scale_factor), _ CInt(bm_source.Height * scale_factor)) ' Make a Graphics object for the result Bitmap. Dim gr_dest As Graphics = Graphics.FromImage(bm_dest) ' Copy the source image into the destination bitmap. gr_dest.DrawImage(bm_source, 0, 0, _ bm_dest.Width + 1, _ bm_dest.Height + 1) ' Display the result. picDest.Image = bm_dest End Sub [Sunting] Satu lagi pada baris yang sama. 14 27 Jan 2010 Binoj Antony
  • 7. The most convenient and reliable file storage service Receive your personal cloud storage with 2Gb of space for free Anda cukup menggunakan kode satu baris ini untuk mengubah ukuran gambar Anda dalam visual basic .net Public Shared Function ResizeImage(ByVal InputImage As Image) As Image Return New Bitmap(InputImage, New Size(64, 64)) End Function Dimana; 1. "InputImage"adalahgambaryanginginAndaubahukurannya. 2. "64 X 64" adalahukuran yangdiperlukan,Andadapatmengubahnyasesuaikebutuhan,mis. 32X32 dll. 16 18 Mar 2014 MuhammadSaqib Ini akan mengubah ukuran gambar apa pun menggunakan kualitas terbaik dengan dukungan untuk 32bpp dengan alpha. Gambar baru akan memiliki gambar asli berpusat di dalam yang baru dengan rasio aspek asli. #Region " ResizeImage " Public Overloads Shared Function ResizeImage(SourceImage As Drawing.Image, TargetWidth As Int32, TargetHeight As Int32) As Drawing.Bitmap Dim bmSource = New Drawing.Bitmap(SourceImage) Return ResizeImage(bmSource, TargetWidth, TargetHeight) End Function Public Overloads Shared Function ResizeImage(bmSource As Drawing.Bitmap, TargetWidth As Int32, TargetHeight As Int32) As Drawing.Bitmap Dim bmDest As New Drawing.Bitmap(TargetWidth, TargetHeight, Drawing.Imaging.PixelFormat.Format32bppArgb) Dim nSourceAspectRatio = bmSource.Width / bmSource.Height Dim nDestAspectRatio = bmDest.Width / bmDest.Height
  • 8. Dim NewX = 0 Dim NewY = 0 Dim NewWidth = bmDest.Width Dim NewHeight = bmDest.Height If nDestAspectRatio = nSourceAspectRatio Then 'same ratio ElseIf nDestAspectRatio > nSourceAspectRatio Then 'Source is taller NewWidth = Convert.ToInt32(Math.Floor(nSourceAspectRatio * NewHeight)) NewX = Convert.ToInt32(Math.Floor((bmDest.Width - NewWidth) / 2)) Else 'Source is wider NewHeight = Convert.ToInt32(Math.Floor((1 / nSourceAspectRatio) * NewWidth)) NewY = Convert.ToInt32(Math.Floor((bmDest.Height - NewHeight) / 2)) End If Using grDest = Drawing.Graphics.FromImage(bmDest) With grDest .CompositingQuality = Drawing.Drawing2D.CompositingQuality.HighQuality .InterpolationMode = Drawing.Drawing2D.InterpolationMode.HighQualityBicubic .PixelOffsetMode = Drawing.Drawing2D.PixelOffsetMode.HighQuality .SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias .CompositingMode = Drawing.Drawing2D.CompositingMode.SourceOver .DrawImage(bmSource, NewX, NewY, NewWidth, NewHeight) End With End Using Return bmDest End Function #End Region 4 11 Mar 2013 Carter Medlin Tidak tahu banyak sintaks VB.NET tapi di sini dan idenya Dim source As New Bitmap("C:image.png") Dim target As New Bitmap(size.Width, size.Height, PixelFormat.Format24bppRgb) Using graphics As Graphics = Graphics.FromImage(target) graphics.DrawImage(source, new Size(48, 48)) End Using 2 27 Jan 2010
  • 9. Alex LE Dim x As Integer = 0 Dim y As Integer = 0 Dim k = 0 Dim l = 0 Dim bm As New Bitmap(p1.Image) Dim om As New Bitmap(p1.Image.Width, p1.Image.Height) Dim r, g, b As Byte Do While x < bm.Width - 1 y = 0 l = 0 Do While y < bm.Height - 1 r = 255 - bm.GetPixel(x, y).R g = 255 - bm.GetPixel(x, y).G b = 255 - bm.GetPixel(x, y).B om.SetPixel(k, l, Color.FromArgb(r, g, b)) y += 3 l += 1 Loop x += 3 k += 1 Loop p2.Image = om
  • 10. Public Sub AutosizeImage(ByVal ImagePath As String, ByVal picBox As PictureBox, Optional ByVal pSizeMode As PictureBoxSizeMode = PictureBoxSizeMode.CenterImage) Try picBox.Image = Nothing picBox.SizeMode = pSizeMode If System.IO.File.Exists(ImagePath) Then Dim imgOrg As Bitmap Dim imgShow As Bitmap Dim g As Graphics Dim divideBy, divideByH, divideByW As Double imgOrg = DirectCast(Bitmap.FromFile(ImagePath), Bitmap) divideByW = imgOrg.Width / picBox.Width divideByH = imgOrg.Height / picBox.Height If divideByW > 1 Or divideByH > 1 Then If divideByW > divideByH Then divideBy = divideByW Else divideBy = divideByH End If imgShow = New Bitmap(CInt(CDbl(imgOrg.Width) / divideBy), CInt(CDbl(imgOrg.Height) / divideBy)) imgShow.SetResolution(imgOrg.HorizontalResolution, imgOrg.VerticalResolution) g = Graphics.FromImage(imgShow) g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic g.DrawImage(imgOrg, New Rectangle(0, 0, CInt(CDbl(imgOrg.Width) / divideBy), CInt(CDbl(imgOrg.Height) / divideBy)), 0, 0, imgOrg.Width, imgOrg.Height, GraphicsUnit.Pixel) g.Dispose() Else imgShow = New Bitmap(imgOrg.Width, imgOrg.Height) imgShow.SetResolution(imgOrg.HorizontalResolution, imgOrg.VerticalResolution) g = Graphics.FromImage(imgShow) g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic g.DrawImage(imgOrg, New Rectangle(0, 0, imgOrg.Width, imgOrg.Height), 0, 0, imgOrg.Width, imgOrg.Height, GraphicsUnit.Pixel) g.Dispose() End If imgOrg.Dispose() picBox.Image = imgShow Else picBox.Image = Nothing End If Catch ex As Exception MsgBox(ex.ToString) End Try End Sub
  • 11. This chapter is from the book Visual BasicProgrammer'sGuide tothe .NET FrameworkClassLibrary Learn More Buy This chapter is from the book Visual BasicProgrammer'sGuide tothe .NET FrameworkClassLibrary Learn More Buy Working with Images In this section, you will see how to use two of the most common graphic types a programmer interacts with: bitmaps and icons. As stated earlier in the chapter, all user-interface objects in Windows are some form of a bitmap; a bitmap is simply a collection of pixels set to various colors. Images The namespace library gives us three classes for working with images: Image, Bitmap and Icon. Image is simply the base class from which the others inherit. Bitmap allows us to convert a graphics file into the native GDI+ format (bitmap). This class can be used to define images as fill patterns, transform images for display, define the look of a button—its uses are many. Although the bitmap format is used to manipulate images at the pixel level, GDI+ can actually work with the following image types:  Bitmaps (BMP)  Graphics Interchange Format (GIF)  Joint Photographic Experts Group (JPEG)  Exchangeable Image File (EXIF)  Portable Network Graphics (PNG)  Tag Image File Format (TIFF) Creating an instance of Bitmap requires a filename, stream, or another valid Image instance. For example, the following line of code will instantiate a Bitmap object based on a JPEG file: Dim myBitmap As New System.Drawing.Bitmap(fileName:="Sample.jpg")
  • 12. Once instantiated, we can do a number of things with the image. For instance, we can change its resolution with the SetResolution method or make part of the image transparent with MakeTransparent. Of course, we will also want to draw our image to the form. We use the DrawImage method of the Graphics class to output the image to the screen. The DrawImage method has over 30 overloaded parameter sets. In its simplest form, we pass the method an instance of Bitmap and the upper-left coordinate of where we want the method to begin drawing. For example: myGraphics.DrawImage(image:=myBitmap, point:=New Point(x:=5, y:=5)) Scaling and Cropping It is often helpful to be able to scale or crop an image to a different size. Suppose you need a 100 x 100 image to fit in a 20 x 20 space, or you want to give your users the ability to zoom in on a portion of an image. You use a variation of the DrawImage method to scale images. This overloaded method takes a Rectangle instance as the destination for drawing your image. However, if the rectangle is smaller or larger than your image, the method will automatically scale the image to match the bounds of the rectangle. Another version of the DrawImage method takes both a source rectangle and a destination rectangle. The source rectangle defines the portion of the original image to be drawn into the destination rectangle. This, effectively, is cropping. The source rectangle defines how the image gets cropped when applied to the destination. Of course, you can crop to the original size or scale the cropped portion to a new size. Listing 9.8 provides a detailed code example of both scaling and cropping an image. Listing 9.8 Scale and Crop Protected Overrides Sub OnClick(ByVal e As System.EventArgs) 'local scope Dim myBitmap As System.Drawing.Bitmap Dim myGraphics As Graphics Dim mySource As Rectangle Dim myDestination As Rectangle 'create an instance of bitmap based on a file myBitmap = New System.Drawing.Bitmap(fileName:="dotnet.gif") 'return the current form as a drawing surface myGraphics = Graphics.FromHwnd(ActiveForm().Handle) 'define a rectangle as the size of the original image (source) mySource = New Rectangle(x:=0, y:=0, Width:=81, Height:=45) 'draw the original bitmap to the source rectangle myGraphics.DrawImage(image:=myBitmap, rect:=mySource) 'create a destination rectangle myDestination = New Rectangle(x:=90, y:=0, Width:=162, Height:=90) 'output the image to the dest. rectangle (scale) myGraphics.DrawImage(image:=myBitmap, rect:=myDestination)
  • 13. 'output a cropped portion of the source myGraphics.DrawImage(image:=myBitmap, _ destRect:=New Rectangle(x:=0, y:=100, Width:=30, Height:=30), _ srcRect:=New Rectangle(x:=0, y:=35, Width:=14, Height:=14), _ srcUnit:=GraphicsUnit.Pixel) End Sub Notice that we actually drew the image to the form three times. The first time, we drew the image into a rectangle (mySource) based on its original size. The second time, we scaled the image to two times its original size (myDestination) by creating a larger rectangle and outputting the image accordingly. Finally, we cropped a portion of the original output and put it in a new, larger rectangle. Figure 9.5 shows the code's output to the form. Figure 9.5 Scale and crop output. Icons An icon in Windows is a small bitmap image that represents an object. You cannot go far in without seeing and working with icons. For example, the File Explorer uses icons to represent folders and files; your desktop contains icons for My Computer, Recycle Bin, and My Network Places. We use the Icon class to work with icons in .NET. We can instantiate an Icon instance in much the same way we created Bitmap objects. The following code creates an icon based on a file name: Dim myIcon as New Icon(fileName:="myIcon.ico") The DrawIcon method of the Graphics class is used to draw the icon to the form. To it, you can pass the icon and either just the upper-left x and y coordinates or a bounding rectangle. If you pass a Rectangle instance, the icon will be scaled based on the bounding rectangle. The following line of code draws an icon object into a bounding rectangle. myGraphics.DrawIcon(icon:=myIcon, _ rectangle:=New Rectangle(x:=5, y:=5, width:=32, height:=32)) The Graphics class also gives us the DrawIconUnstretched method that allows us to specify a bounding rectangle without actually scaling the icon. In fact, if the icon is larger than the bounding rectangle, it will be cropped to fit from the left corner down and to the right. Suggestions for Further Exploration  To animate images, take a look at the ImageAnimator class.  Check out the SmoothingMode property of the Graphics class and the SmoothingMode enumeration members. This method allows you to set things like antialiasing to make your graphics look "smoother."