Archive for January, 2006

Class to create thumbnail images (VB.Net, GDI.Net)

A class written in VB.Net using GDI.net to create thumbnail images and stream or save them as JPG.

Public Class JPGThumbnail
 
    Private Enum Method
        File
        Stream
    End Enum
 
    Private Sub New()
    End Sub
 
    Public Shared Sub CreateThumbnail(ByVal InputFilePath As String, _
                ByVal intMaxLen As Integer, _
                ByVal OutputStream As IO.Stream)
        CreateThumbnail(InputFilePath, intMaxLen, Method.Stream, , OutputStream)
    End Sub

    Public Shared Sub CreateThumbnail(ByVal InputFilePath As String, _
                    ByVal intMaxLen As Integer, _
                    ByVal OutputFilePath As String)
        CreateThumbnail(InputFilePath, intMaxLen, Method.File, OutputFilePath)
    End Sub
 
    Private Shared Sub CreateThumbnail(ByVal InputFilePath As String, _
                    ByVal intMaxLen As Integer, _
                    ByVal ThumbnailMethod As Method, _
                    Optional ByVal OutputFilePath As String = "", _
                    Optional ByVal OutputStream As IO.Stream = Nothing)
        Dim fileInfo As New IO.FileInfo(InputFilePath)
 
        If fileInfo.Exists() Then
 
            Dim bmp As System.Drawing.Bitmap
            Dim bmpThumb As System.Drawing.Bitmap
 
            Try
                bmp = New System.Drawing.Bitmap(InputFilePath)
                Dim intWidth As Integer = bmp.Width
                Dim intHeight As Integer = bmp.Height
                Dim intThumbWidth As Integer
                Dim intThumbHeight As Integer
 
                If intWidth > intHeight Then
                    intThumbWidth = intMaxLen
                    intThumbHeight = (intHeight / intWidth) * intMaxLen
                Else
                    intThumbHeight = intMaxLen
                    intThumbWidth = (intWidth / intHeight) * intMaxLen
                End If
 
                Dim thumbSize As New Drawing.Size(intThumbWidth, intThumbHeight)
                bmpThumb = New System.Drawing.Bitmap(bmp, thumbSize)
 
                If intMaxLen >= intHeight Or intMaxLen >= intWidth Then
                    ‘ Save the original
                    If ThumbnailMethod = Method.File Then
                        bmp.Save(OutputFilePath, System.Drawing.Imaging.ImageFormat.Jpeg)
                    Else
                        bmp.Save(OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
                    End If
                Else
                    ‘ Save the thumbnail version
                    If ThumbnailMethod = Method.File Then
                        bmpThumb.Save(OutputFilePath, System.Drawing.Imaging.ImageFormat.Jpeg)
                    Else
                        bmpThumb.Save(OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
                    End If
 
                End If
 
            Catch ex As Exception
                Throw
                ‘WriteErrorMsg(OutputFilePath)
            Finally
                ‘clean-up…
                bmp.Dispose()
                bmpThumb.Dispose()
            End Try
 

        Else
            Throw New ApplicationException(”Input file [” & InputFilePath & ”] does not exist.”)
        End If
 
    End Sub

    ‘* This can be used to return an image even in an error condition, if that’s what you want.
    ‘Private Shared Sub WriteErrorMsg(ByVal OutputFilePath As String)
 
    ‘    Dim bmp As New System.Drawing.Bitmap(100, 100, System.Drawing.Imaging.PixelFormat.Format16bppRgb565)
    ‘    ’ Get the underlying Graphics object.
    ‘    Dim gr As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bmp)
    ‘    ’ Clear its background.
    ‘    gr.Clear(System.Drawing.Color.WhiteSmoke)
 
    ‘    ’ create a font
    ‘    Dim fnt As New System.Drawing.Font(”Arial”, 8, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point)
 
    ‘    gr.DrawString(”Not”, fnt, System.Drawing.Brushes.Blue, 36, 30)
    ‘    gr.DrawString(”Available”, fnt, System.Drawing.Brushes.Blue, 23, 50)
 
    ‘    Dim penRect As New System.Drawing.Pen(Color.LightBlue, 2)
    ‘    gr.DrawRectangle(penRect, 1, 1, 98, 98)
 
    ‘    ’ Release resources.
    ‘    fnt.Dispose()
    ‘    gr.Dispose()
 
    ‘    bmp.Save(OutputFilePath, System.Drawing.Imaging.ImageFormat.Jpeg)
    ‘    bmp.Dispose()
    ‘End Sub
 

End Class

Note: ‘Code to make use of it…
JPGThumbnail.CreateThumbnail(”C:\DCP02396.JPG”, 100, “C:\DCP02396_small.JPG”)
Dim outputStream As New IO.FileStream(”C:\DCP02396_small2.jpg”, IO.FileMode.CreateNew)
JPGThumbnail.CreateThumbnail(”C:\DCP02396.JPG”, 100, outputStream)
outputStream.Flush()
outputStream.Close()

1 comment January 22nd, 2006

VB Script to list all running processes including the folder they are running from

Every wonder about all those processes in the Task Manager list of processes are? This is a quick and dirty way of getting a list of those processes, the file they’re running, and a number of other attributes.

I find it very useful to know the path and filename that is creating the processes in the list.

This is one way.. like I said, quick and dirty.. to detect and possibly help remove spyware, adware, and malware in general.  It’s also useful to help debug system performance issues.

On Error Resume Next
Dim LOG_FILE
Dim strYear, strMonth, strDay, intChangedNotes
strYear = Year(Now())
strMonth = Month(Now())
strDay = Day(Now())
LOG_FILE = "Processes_" & strYear & PadLeftZero(strMonth, 2) & PadLeftZero(strDay, 2) & ".txt"

s = ""
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Process",,48)
For Each objItem in colItems
s = s & vbCrLf & "-------------------------------------" & vbCrLf
s = s & "Caption: " & objItem.Caption & vbcrlf
s = s & "ExecutablePath: " & objItem.ExecutablePath & vbcrlf
s = s & "ProcessId: " & objItem.ProcessId & vbcrlf
s = s & "ParentProcessId: " & objItem.ParentProcessId & vbcrlf
s = s & "WorkingSetSize: " & objItem.WorkingSetSize & vbcrlf
s = s & "Mem Usage: " & objItem.WorkingSetSize/1024 & " K" & vbcrlf
s = s & vbCrLf & vbCrLf
Next

WriteToFile s, LOG_FILE

WScript.Echo "Done."

Function PadLeftZero(ByVal istrExpression, ByVal iintMaxLen)

PadLeftZero = PadLeft(istrExpression, "0", iintMaxLen)

End Function

Public Function PadLeft(ByVal istrSource, ByVal istrPadChar, ByVal iintMaxLen)
' Append istrSource to the end of a string of istrPadChar and return
' a string no longer than iintMaxLen.
PadLeft = Right(String(iintMaxLen, Left(istrPadChar, 1)) & istrSource, iintMaxLen)
End Function

Sub WriteToFile(istrNote, istrLogFile)
Const ForReading = 1, ForWriting = 2, ForAppending = 8

dim objFSO
dim objFile
dim objTS

set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTS = objFSO.OpenTextFile(istrLogFile, ForWriting, True)
objTS.WriteLine istrNote
objTS.Close
set objTS = Nothing
set objFSO = Nothing
End Sub

Note: This script writes the results to a file, but you could just as easily do a WScript.Echo of each process you're interested in.

Note: This script writes the results to a file, but you could just as easily do a WScript.Echo of each process you're interested in.

 

2 comments January 20th, 2006

Attach a file to an email with VBScript

A VBScript that sends an email with a file attachment.   I’ve used this in Windows 2000 (Win2K) and Windows XP Pro (XP Pro) environments.eg. Drag a file from explorer onto the icon for this script and it will send that file as an attachment to the email address you specify.

eg. Or.. incorporate it into an automated process by passing the filename and email address as parameters. This is useful if you want to send a file with a scheduled task or job.

Dim oArgs, i, sSubject, sBody, sEmail
Dim fromEmail, fileName, toEmail, smtpServer
 

smtpServer = ""
fromEmail = "Daemon@domain.com"

Set oArgs = WScript.Arguments

If oArgs.Count < 1 Then
     WScript.Echo "Drag a file onto this icon to email it." & vbcrlf & vbcrlf & "You will be prompted for an email address to send to."
     WScript.Quit
End If

fileName = oArgs(0)

If oArgs.Count <> 2 Then
     toEmail = InputBox(”Please type in the recipient’s email:”, “Send to?”, “;@domain.com”)
Else
     toEmail = oArgs(1)
End If

if fileName <> “” and fromEmail <> “” and toEmail <> “” then

     sSubject = “File from [” & fromEmail & “]”
     sBody = “See attached file [” & GetFilenameOnly(fileName) & “]”
     sEmail = toEmail
     
     MailMe_CDOSys sSubject, sBody, sEmail, fileName
     If oArgs.Count <> 2 Then WScript.Echo “Mail sent…”

else
     If oArgs.Count <> 2 Then WScript.Echo “Incorrect parameters.. no email sent”

end if

Sub MailMe_CDOSYS (s, b, email, attachment)
     On Error Resume Next
     Dim o
     Set o = CreateObject(”CDO.Message”)
     o.From = fromEmail
     o.Sender = “MailMeAttachment”
     o.To = email
     o.Subject = s
     o.AddAttachment fileName
     o.TextBody = b
     o.DNSOptions = 0
     o.ConfigurationFields(”http://schemas.microsoft.com/cdo/configuration/smtpserver”).Value = smtpServer
     o.ConfigurationFields(”http://schemas.microsoft.com/cdo/configuration/sendusing”).Value = 2     ’send using network
     o.ConfigurationFields(”http://schemas.microsoft.com/cdo/configuration/SMTPConnectionTimeout”).Value = 30
     o.ConfigurationFields(”http://schemas.microsoft.com/cdo/configuration/SMTPServerPort”).Value = 25
     o.ConfigurationFields(”http://schemas.microsoft.com/cdo/configuration/SMTPAuthenticate”).Value = 0
     o.Configuration.Fields.Update
     o.Fields.Update
     o.send
     Set o = Nothing
End Sub

Function GetFilenameOnly(s)
     Dim sRet, iLast
     iLast = InStrRev(s, “\”)
     sRet = Right(s, len(s)-iLast)
     GetFilenameOnly = sRet
End Function

Add comment January 19th, 2006

VB Script / Windows Script function used to log to the application event log

Here is a very useful set of functions if you want to log to the application event log in Windows.  I’ve used it on scheduled scripts, jobs, HTA (HTML Application).

You can call the LogEvent function with a string that you want to place in the application event log.  This defaults as an information event.  You can optionally call the LogEvent_ex, LogEventAudit, or LogEventAuditFail functions to specify a different event type.  Enjoy!

Sub LogEvent (sNote)
     LogEvent_Ex 4, sNote
end sub

Sub LogEventAudit (sNote)
     LogEvent_Ex 8, sNote
End Sub

Sub LogEventAuditFail (sNote)
     LogEvent_Ex 16, sNote
End Sub

Sub LogEvent_Ex(i, sNote)
     ‘*Constants:
     ‘     1=Error
     ‘     2=Warning
     ‘     4=Information
     ‘     8=Audit Success
     ‘     16=Audit Failure

     dim ws
     set ws = CreateObject(”WScript.Shell”)
     ws.LogEvent i, sNote
     set ws = Nothing
End Sub

Add comment January 17th, 2006

Indexing Service Details (as HTML report)

This script generates an HTML report sent by email of the status and health of specified MS Indexing Service catalogs using a COM object in VBScript.

MS Content Index Service (CISVC) can be accessed through the object Microsoft.ISAdm.

Download the script to generate the CISVC report

Add comment January 16th, 2006


Calendar

January 2006
M T W T F S S
« Sep   May »
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

Posts by Month

Posts by Category