11/25/2011

VB 2008 :: ลดขนาด size ของรูป ก่อนเก็บ

เป็นการลดขนาด size ไม่ได้ลดขนาดความกว้างความสูงของภาพ ครับ

            Dim percentResize As Double
Dim bm As New Bitmap(TextBox_URL.Text) 'ที่อยู่ Part และชื่อไฟล์ที่จะ ลดขนาด
Dim width As Integer = bm.Width - (bm.Width * percentResize) 'image width.                Dim height As Integer = bm.Height - (bm.Height * percentResize)  'image height
Dim thumb As New Bitmap(width, height)
Dim g As Graphics = Graphics.FromImage(thumb)
g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
g.DrawImage(bm, New Rectangle(0, 0, width, height), New Rectangle(0, 0, bm.Width, _
bm.Height), GraphicsUnit.Pixel)
g.Dispose()
bm.Dispose()

'image path New.
thumb.Save(TextBox_URL.Text, _
System.Drawing.Imaging.ImageFormat.Jpeg) 'can use any image format
thumb.Dispose()


**** เวลานำมาแสดงในตัวโปรแกรม VB โดยใช้ PictureBox
ให้กำหนด SizeMode เป็น StretchImage เพื่อให้รูปพอดี กับกรอบที่กำหนดไว้

ที่มา http://www.thedesilva.com/2010/01/resize-image-using-vb-net/

No comments:

Post a Comment