Resize images automatically through vb.net? - TalishHF - 01-17-2012
How would I go about making a program that resizes every image in a specific file to a specified size, not file size, actual image size like X Y size?
RE: Resize images automatically through vb.net? - AceInfinity - 01-17-2012
Do you care about constraining proportions?
(01-17-2012, 09:52 AM)TalishHF Wrote: How would I go about making a program that resizes every image in a specific file to a specified size, not file size, actual image size like X Y size?
Every image in a specific file? I thought there was only one file inside every file?
"Directory" maybe?
RE: Resize images automatically through vb.net? - AceInfinity - 01-17-2012
Created a demo for you:
Code: Imports System.IO, System.Drawing.Imaging
Public Class Form1
Private InputDir As String = Application.StartupPath & "\"
Private SaveDir As String = Application.StartupPath & "\Resized Images Dir\"
Private ResizePx As Integer = 250
Private CalcSizeW, CalcSizeH As Integer
Private ValidExt() As String = {".bmp", ".jpg", ".png", ".gif"}
Private ExtArr As New List(Of String)
Private Sub NewImage(ByVal FilePath As String, ByVal ExtType As String)
If Not Directory.Exists(SaveDir) Then Directory.CreateDirectory(SaveDir)
Dim bmp As New Bitmap(FilePath)
If Width < Height Then
CalcSizeH = ResizePx
CalcSizeW = (bmp.Width * ResizePx) \ bmp.Height
ElseIf Width > Height Then
CalcSizeW = ResizePx
CalcSizeH = (bmp.Height * ResizePx) \ bmp.Width
Else
'it's a square image so put both dimensions to ResizePx
Width = Height = ResizePx
End If
Dim ResizeImg As New Bitmap(CalcSizeW, CalcSizeH)
Dim g As Graphics = Graphics.FromImage(ResizeImg)
With g
.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
.DrawImage(bmp, New Rectangle(0, 0, Width, Height), New Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel)
.Dispose()
End With
bmp.Dispose()
Dim FName As String = SaveDir & "Resized 1" & ExtType
Dim Num As Integer = 1
While File.Exists(FName)
Num += 1
FName = SaveDir & "Resized " & Num & ExtType
End While
Select Case ExtType.ToLower
Case Is = ".jpg"
ResizeImg.Save(FName, ImageFormat.Jpeg)
Case Is = ".png"
ResizeImg.Save(FName, ImageFormat.Png)
Case Is = ".bmp"
ResizeImg.Save(FName, ImageFormat.Bmp)
Case Is = ".gif"
ResizeImg.Save(FName, ImageFormat.Gif)
End Select
ResizeImg.Dispose()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ExtArr.AddRange(ValidExt)
For Each File As String In Directory.GetFiles(InputDir)
Dim FileExt As String = File.Substring(File.LastIndexOf("."), File.Length - File.LastIndexOf(".")).ToLower
If ExtArr.Contains(FileExt) Then
NewImage(File, FileExt)
End If
Next
MessageBox.Show("Done resizing images", "Action completed", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
End Sub
End Class
RE: Resize images automatically through vb.net? - CamilleM - 04-10-2013
hi ,you wanna resize image with VB.NET?have a look at VB.NET Codes for Resizing Images .i have found that by google.hope that it is what you want
|