Monday, May 14, 2012

Why Nikola Tesla was the greatest geek who ever lived - The Oatmeal

Why Nikola Tesla was the greatest geek who ever lived - The Oatmeal

Thursday, May 3, 2012

Create Shortcuts using late binding

Simply create a new shortcut using some late binding, instead of having to reference the Windows Scripting Host : IWshRuntimeLibrary

Usage:
Shortcut.Create_Shortcut(PathtoExecutable, PathToShortcutFolder, ShortCutName,ExecutableFolderPath, Description, "", 0, 0)

 
Imports System.Runtime.CompilerServices
Imports Microsoft.VisualBasic.CompilerServices
Public Class Shortcut
Public Shared Function Create_Shortcut(ByVal Target_Path As String, ByVal Shortcut_Path As String, ByVal Shortcut_Name As String, _
ByVal Working_Directory As String, ByVal Description As String, ByVal Arguments As String, ByVal Window_Style As Integer, ByVal Icon_Num As Integer) As Boolean
Try
Dim objectValue As Object = RuntimeHelpers.GetObjectValue(Interaction.CreateObject("WScript.Shell", ""))
Dim objectValue2 As Object = RuntimeHelpers.GetObjectValue(NewLateBinding.LateGet(objectValue, Nothing, _
"CreateShortcut", New Object() {Shortcut_Path & "\" & Shortcut_Name & ".lnk"}, Nothing, Nothing, Nothing))
NewLateBinding.LateSet(objectValue2, Nothing, "TargetPath", New Object() {Target_Path}, Nothing, Nothing)
NewLateBinding.LateSet(objectValue2, Nothing, "WorkingDirectory", New Object() {Working_Directory}, Nothing, Nothing)
NewLateBinding.LateSet(objectValue2, Nothing, "Arguments", New Object() {Arguments}, Nothing, Nothing)
NewLateBinding.LateSet(objectValue2, Nothing, "WindowStyle", New Object() {Window_Style}, Nothing, Nothing)
NewLateBinding.LateSet(objectValue2, Nothing, "Description", New Object() {Description}, Nothing, Nothing)
NewLateBinding.LateSet(objectValue2, Nothing, "IconLocation", New Object() {Target_Path & "," _
& Conversions.ToString(Icon_Num)}, Nothing, Nothing)
NewLateBinding.LateCall(objectValue2, Nothing, "Save", New Object() {}, Nothing, Nothing, Nothing, True)
Return True
Catch ex As Exception
Return False
End Try
End Function
End Class

A special thanks to Tirthraaj Gobin for comming up with this solution...
http://social.msdn.microsoft.com/Forums/en-US/vbide/thread/126c09a2-6f38-4747-9ddd-55ff977fcba0?prof=required

Wednesday, May 2, 2012

PDF Layer Merger

 

Recently I’ve created a simple .NET tool, to merge 2 PDF files, one as a layer on top of the other.

The reason behind this, was to be able to digitally remark/comment/mark-up on a PDF file using AutoCAD.  People tend to print out a PDF file, use some highlighters and draw some clouds and comments on this PDF, only to re-scan this afterwards.
This is the way we used to work, but today we live in a digital era…

The tool has been built in VB.NET targeting the NET 2.0 framework, with the use of the excellent PDF library iTextSharp (based on iText).

image


Snippet of the actual transformation function:

  
'''
''' Combines two source pdf's to one single pdf.
'''

''' The file-location incl. the name of the original pdf.
''' The file-location incl. the name of the mark-up pdf.
''' The file-location incl. the name of the generated combined pdf.
''' The name which the added mark-up layer will have in the new pdf.
''' The transformation which is executed on the original pdf: rotation, scaleX, scaleY, offsetX, offsetY
''' Return boolean indication whether the combination of the pdf's was successful.
'''
Public Function PDFCombineLayer(ByVal sourceOriginal As String, ByVal sourceMarkup As String, ByVal destination As String, Optional ByVal NewLayer As String = "SOURCE-DRAWING", Optional ByVal Transform As String = "0;1;1;0;0") As Boolean
Dim oPdfOriginal As iTextSharp.text.pdf.PdfReader = Nothing
Dim oPdfMarkup As iTextSharp.text.pdf.PdfReader = Nothing
Dim oPdfStamper As iTextSharp.text.pdf.PdfStamper = Nothing
Dim PDFDirectContent As iTextSharp.text.pdf.PdfContentByte
Dim iNumberOfPages As Integer = Nothing
Dim iPage As Integer = 0
Dim matrix1 As Drawing2D.Matrix = Nothing
Dim matrix2 As Drawing2D.Matrix = Nothing

Try
'Read the source pdf-files and prepare new file.
oPdfOriginal = New iTextSharp.text.pdf.PdfReader(sourceOriginal)
oPdfMarkup = New iTextSharp.text.pdf.PdfReader(sourceMarkup)
oPdfStamper = New iTextSharp.text.pdf.PdfStamper(oPdfMarkup, New IO.FileStream(destination, IO.FileMode.Create, IO.FileAccess.Write))

'Count pages in original pdf and create new pdf wih same amount of pages.
iNumberOfPages = oPdfOriginal.NumberOfPages
oPdfStamper.GetPdfLayers()

'Make transformation matrix
If Transform = "" Then Transform = "0;1;1;0;0" 'Note transform: rotation(degrees), scaleX, scaleY, offsetX, offsetY
Dim ar() As String = Split(Transform, ";")
If Not ar.Length = 5 Then ar = New String() {0, 1, 1, 0, 0}
If ar(1) <= 0 Or ar(2) <= 0 Then Throw New ArgumentOutOfRangeException("transform", "Make sure that scaling factors of the transformation are larger than zero.")
matrix1 = New Drawing2D.Matrix
matrix1.Rotate(-CSng(ar(0)))
matrix1.Scale(CSng(ar(1)), CSng(ar(2)))
matrix1.Translate(CSng(ar(3)), CSng(ar(4)))
matrix2 = New Drawing2D.Matrix

'Add the new layer to each of the pdf-pages.
If NewLayer = "" Then NewLayer = "SOURCE-DRAWING"
Dim PDFNewLayer As New iTextSharp.text.pdf.PdfLayer(NewLayer, oPdfStamper.Writer)
Dim PDFLayerMem As New iTextSharp.text.pdf.PdfLayerMembership(oPdfStamper.Writer)
PDFLayerMem.AddMember(PDFNewLayer)
Do While (iPage < iNumberOfPages)
'Get new page
iPage += 1
Dim iPageOriginal As iTextSharp.text.pdf.PdfImportedPage = oPdfStamper.Writer.GetImportedPage(oPdfOriginal, iPage)
PDFDirectContent = oPdfStamper.GetUnderContent(iPage) 'add page under existing pdf in new layer
matrix2 = matrix1.Clone 'Cloning is necessary, otherwise all adaptations to matrix2 are also done in matrix1.

'Compensate for the orientation of the original pdf-file.
Dim iRotation As Single = oPdfOriginal.GetPageRotation(iPage)
Select Case iRotation
Case Is = 90
matrix2.Rotate(-iRotation)
matrix2.Translate(-oPdfOriginal.GetPageSize(iPage).Width, 0)
Case Is = 180 'Not tested yet.
matrix2.Rotate(-iRotation)
matrix2.Translate(-oPdfOriginal.GetPageSize(iPage).Width, -oPdfOriginal.GetPageSize(iPage).Height)
Case Is = 270 'Not tested yet.
matrix2.Rotate(-iRotation)
matrix2.Translate(0, oPdfOriginal.GetPageSize(iPage).Height)
Case Else
End Select

'Add the new layer ot this page.
PDFDirectContent.BeginLayer(PDFNewLayer)
PDFDirectContent.AddTemplate(iPageOriginal, matrix2)
PDFDirectContent.EndLayer()
Loop
oPdfStamper.SetFullCompression()
Return True
Catch ex As Exception
Return False
Finally
oPdfMarkup.Close()
oPdfOriginal.Close()
oPdfStamper.Close()
matrix1.Dispose()
matrix2.Dispose()
End Try
End Function

Code has been posted on Google Code http://code.google.com/p/pdflayermerger/


Many thanks to iText for the excellent library and to Lukasz Swiatkowski for the glass button component (http://www.codeproject.com/Articles/17695/Creating-a-Glass-Button-using-GDI).

Thursday, April 26, 2012

Apple is 10 years behind Microsoft when it comes to security. Time to face the music…

 

http://malware.cbronline.com/news/apple-10-years-behind-microsoft-on-security-kaspersky-250412

Credits goto Steve Evans & CBR Online
http://malware.cbronline.com/archive/4294941974

It seems it’s time to face the music for Apple..

Kaspersky founder and CEO Eugene Kaspersky,
speaking exclusively to CBR at Info Security 2012, Kaspersky told us that Apple is a long way behind Microsoft when it comes to security and will have to change the ways it approaches updates following the recent malware attacks.

"I think they are ten years behind Microsoft in terms of security," Kaspersky told CBR. "For many years I've been saying that from a security point of view there is no big difference between Mac and Windows. It's always been possible to develop Mac malware, but this one was a bit different. For example it was asking questions about being installed on the system and, using vulnerabilities, it was able to get to the user mode without any alarms."

Kaspersky added that his company is seeing more and more malware aimed at Macs, which is unsurprising given the huge number of devices being sold. Its most recent quarter revealed Mac sales of 4 million, a 7% rise on the year ago quarter. These figures are still dwarfed by PC sales of course, and Kaspersky said Windows will remain the primary target for cyber criminals.

However he added an increase in Mac malware was, "just a question of time and market share. Cyber criminals have now recognised that Mac is an interesting area. Now we have more, it's not just Flashback or Flashfake. Welcome to Microsoft's world, Mac. It's full of malware."

"Apple is now entering the same world as Microsoft has been in for more than 10 years: updates, security patches and so on," he added. "We now expect to see more and more because cyber criminals learn from success and this was the first successful one."

Tuesday, April 17, 2012

Springpad : Make your live easier

Springpad helps you share & discover with the people you trust.

Create notebooks for recipes, books, movies or anything else that matters to you, together with friends, family and co-workers. Save ideas and info from anywhere, access them whenever, and start getting more from life.
Get Started



Collaborate with your friends and family.



Start a notebook for any interest, any project, any “I’ve got to do this!” list. Then invite the right people to help with each one – your foodie friends, your book club buddies, your mom – anyone! Comment on each other’s contributions and build something great together.


Save anything to Springpad, from anywhere.

Fill your Springpads with things you find on the web or on the go. Clip an article, snap a photo, scan a product barcode, record a voice memo, or save a place nearby. Then access it anytime, anywhere you need it.


Whatever you save, we’ll make even better.

Add something to your Springpad, and we’ll instantly enhance it with more information. Save a restaurant and we’ll give you a map and reviews. Save a movie, and we’ll give you the showtimes near you. Save a book, and we’ll link you to where you can buy it. Save a product, we’ll tell you when there’s a price drop. Get the idea?




Monday, April 16, 2012

Chronology of Programming Languages

Found a nice article check it out : The Semicolon Wars » American Scientist





A chronology of selected programming languages shows a few of the links between them. The diagram is not a genealogy but merely indicates major patterns of influence. The classification of languages as imperative, functional, object-oriented or declarative is also approximate; only a few "pure" languages belong exclusively to one of these categories. The chronology is based in part on time lines constructed by Éric Lévénez and by Pascal Rigaux and on information from the Association for Computing Machinery History of Programming Languages conferences.
Brian Hayes

Saturday, October 29, 2011

Getting Mimetype .NET


Private Function MimeType(Filename As String) As String
 Dim mime As String = "application/octetstream"
 Dim ext As String = System.IO.Path.GetExtension(Filename).ToLower()
 Dim rk As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext)
 If rk IsNot Nothing AndAlso rk.GetValue("Content Type") IsNot Nothing Then
  mime = rk.GetValue("Content Type").ToString()
 End If
 Return mime
End Function
In short - you just feed it with path top a file - and get back the mime type. Ex MimeType("f:\\theimage.jpg") --->>> "image/jpeg"

Sunday, October 16, 2011

Check For 64bit Operating System VB.NET 2.0

    Public Function Is64BitOperatingSystem() As Boolean?
        Dim amount As Integer
        Try
            amount = Runtime.InteropServices.Marshal.SizeOf(IntPtr.Zero)
            If amount = 8 Then : Return True
            Else : Return False
            End If
        Catch ex As Exception
            Return Nothing
        End Try
    End Function 'End Method Is64BitOperatingSystem

Wednesday, October 12, 2011

Count Lines in File


The fastest solution + holds only 1 line at a time in the memory (best solution)

Private Function TotalLines(filePath As String) As Integer
 Using r As New StreamReader(filePath)
  Dim i As Integer = 0
  While r.ReadLine() IsNot Nothing
   i += 1
  End While
  Return i
 End Using
End Function