Thursday, January 22, 2004

Get The Filesize Of A Url's Target File In VB.Net

Not too long ago I wrote a program that had the requirement of displaying the size of a file targetted by a URL without actually downloading the file (the filesize was to help the user determined whether or not they wanted to take the time to download the file).

I wrote a little function for Visual Basic.Net to accomplish this, and found it so darn handy that I thought I would post it here for you. Here it is:

     Public Function GetFileSize(ByVal url As String) As Long


Dim request As System.Net.HttpWebRequest
Dim response As System.Net.HttpWebResponse
Dim fileSize As Long

Try
request = System.Net.WebRequest.Create(url)
response = request.GetResponse()
fileSize = response.ContentLength
response.Close()
Return fileSize
Catch ex As Exception
Return -1
End Try

End Function



Some related properties that you can get from an HttpWebResponse object are:

  • CharacterSet - Gets the character set of the response.

  • ContentEncoding - Gets the method used to encode the body of the response.

  • ContentType - Gets the content type of the response.

  • Cookies - Gets or set cookies associated with the response.

  • LastModified - Gets the last date and time the contents of the response was modified.

  • Method - Gets the method used to return the response.

  • ProtocolVersion - Gets the version of the HTTP protocol used in the response.

  • Server - Gets the name of the server that sent the response.



All of the above information can be extracted from the response without actually having to pull down the entire file.

Wednesday, January 21, 2004

Load And Print A Multipage TIFF From VB.NET

The .NET framework added a groovy wrapper for the GDI+ graphics library. One of the things that this has really helped me with in my job is the handling of TIFFs. In our VB6 applications I had to use a third party control (usually from LEAD) for loading, manipulating and finally printing TIFFs (the principal image format for our client base). No more! .NET has plenty of routines for handling every kind of image manipulation you can imagine: loading, rotating, printing, converting, and drawing. It's great.

When it came to the manipulation of multipage TIFFs, I found that the documentation wasn't easily understandable, and I only found little bits and pieces on the 'net for how to do it. I eventually pieced it all together into something workable, but I wanted to make it a little easier on you. So here's a class that loads and prints a multipage TIFF to the default printer.

Imports System

Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Printing

Public Class PrintTiff

Private m_tiff As Drawing.Image
Private m_fd As FrameDimension

Public Sub New()

End Sub

' Sends a multi-page TIFF document to the default printer.
Public Function PrintTiffDocument(ByVal PathToTIFF As String) As String

Dim tiffDocument As New PrintDocument

Try
' Load TIFF from file.
m_tiff = System.Drawing.Image.FromFile(PathToTIFF)

' Get the frame dimensions.
m_fd = New FrameDimension(m_tiff.FrameDimensionsList(0))

' Setup the print handler.
AddHandler tiffDocument.PrintPage, AddressOf PrintTiffPage

' Print the image.
tiffDocument.Print()
Catch
' Throw all exceptions to caller.
Throw
End Try

End Function

' Print each page of the tiff to the printer.
Private Sub PrintTiffPage(ByVal sender As Object, ByVal e As PrintPageEventArgs)

Static pageNum As Int32 = 0

m_tiff.SelectActiveFrame(m_fd, pageNum)
e.Graphics.DrawImage(m_tiff, New PointF(0, 0))

pageNum += 1
If pageNum < m_tiff.GetFrameCount(m_fd) Then
e.HasMorePages = True
Else
pageNum = 0
End If

End Sub

End Class


-

I'm not sure why, though I'm sure there's a good historic reason, but GDI+ refers to an image page as a "frame". So all of the methods with "frame" in the name are actually used in manipulating the pages. The PrintTiffPage needs a little bit of explaining, so here's what's happening there:

First of all, I keep the current page number in a static variable so that for each subsequent call the method knows which page to print (this number is reset to zero once the image has been completely printed). The SelectActiveFrame() method sets the page number that is the current page for the TIFF (starting with zero). You have to have a FrameDimension object for the TIFF to pass to this method. After selecting the page to print, I draw the page onto the printer using the DrawImage method. I put the page in the upper-left corner of the printer. Finally, if there are any pages left to print, I set the event flag indicating this is the case, otherwise I reset the page number to zero so the next call to the PrintTiffDocument() method will start on the first page.

Questions or comments? Email me!

Tuesday, January 20, 2004

Microsoft vs. MikeRoweSoft

I'll try to post some code later today, but in the mean time check out this interesting article:

MikeRoweSoft garners funds to fight back

Monday, January 19, 2004

How To Disable Browser HotKeys Using JavaScript

I noticed that a number of people were getting to my blog searching at Google for how to disable web browser hotkeys, so I figured I'd make them happy by writing a blog showing how to do this.

NOTICE: I saw in my logs that some people searching for disabling CTRL-P were sent to this page by Google. If you're interested in disabling printing from a web page using JavaScript, see this blog entry instead.

First of all, let me say that disabling hotkeys in a browser is by no means a certain prospect using javascript. I have never had success disabling the alt-keys for built-in browser menu items, and the way the browser handles the hotkeys tends to vary from browser to browser. So...

NOTICE: Do not rely on JavaScript attempts to disable hotkeys as an iron-clad security measure! There's no guarantees!

However, that said, here's a method that works in IE 4 and above. I have the code setup for Netscape as well, just in case you want to use the code to trap keys in Netscape and IE (but they will only be successfully disabled in IE).

function keyTrap(e)

{
// Netscape uses e, IE window.event
var eventObj = (e)?e:window.event;
var keyPressed = (e)?e.which:window.event.keyCode;
var ctrlPressed = (e)?(e.ctrlKey):window.event.ctrlKey;

if (ctrlPressed)
{
switch (keyPressed)
{
case 80: // CTRL-P.
window.event.keyCode = 0;
return false;
}
}
}
if(document.layers)
{
//NS4+
document.captureEvents(Event.ONKEYDOWN);
}
document.onkeydown=keyTrap;


-

This code disables only CTRL-P, which can be useful in preventing printing of a page. It doesn't work at all if the user has Netscape. The key is trapped, but the Print dialog still comes up. Even if you try to set e.which to 0, it still does not work.

SOAPBOX: Herein lies one of the most frustrating aspects of building web applications. You have to have six copies of various versions of browsers available if you want 100% compatability. ASP.NET promised it would take care of this problem, and it does a very good job at doing so for its built-in components. However, despite ASP.NET's greatly expanded capabilities over ASP, I have yet to create a web project that does not need at least some javascript coding. And Microsoft and Netscape bicker and fuss over who's version of javascript is correct. Microsoft is notorious for adding in their own functionality without anybody else's approval, and Netscape is notorious for not duplicating it in the same way (even if they think it's a great idea--it's like they give a property its own name in javascript that is different from the one Microsoft gave it--even if it does exactly the same thing!--just so it will be different from Microsoft).

Okay, off the soapbox.

That's it for today. Feel free to email me with any questions or comments.