Showing posts with label library. Show all posts
Showing posts with label library. Show all posts

Wednesday, 15 August 2018

VB.NET Extending the MailMessage to have the ability to save the mail message

Problem:

The System.Net.Mail.MailMessage is badly missing the option of saving the Message as a file. Though there is a workaournd by using the SpecifiedPickupDirectory option of the SmtpClient to write the mail message ot the specific folder. This option however doesn't have flexibility in naming the files. In a multi-threaded parallel execution environment, it is difficult to identify and rename the specific files post writing to disk. There ought to be a better solution to handle this problem.

Solution:

You can extend the MailMessage class and add a Save method yourself. The Send method of the SmtpClient creates and uses MailWriter object to write out the mail message and it uses fileMailWriter when the SpecificPickupDirectory is specified as the DeliveryMethod. Create your own MailWriter object using FileStream and invoke the internal Send method, passing your MailWriter object. Check out this Code Project Article for more details. The code samples given there are for the C# language. Just in case you need, given below the same code for the Extension in VB.NET.


Imports System
Imports System.Net.Mail
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.IO
Imports System.Reflection
Imports System.Runtime.CompilerServices

Module MailMessageExtension
    <Extension()>
    Public Sub Save(ByVal msg As MailMessage, FileName As String)

        Dim asm As Assembly = New SmtpClient().GetType().Assembly
        Dim _mailWriterType As Type = asm.GetType("System.Net.Mail.MailWriter")


        Using _fileStream As FileStream = New FileStream(FileName, FileMode.Create)


            ' Get reflection info for MailWriter contructor
            Dim _mailWriterContructor As ConstructorInfo = _mailWriterType.GetConstructor(BindingFlags.Instance Or BindingFlags.NonPublic, Nothing, New Type() {GetType(Stream)}, Nothing)

            ' Construct MailWriter object with our FileStream
            Dim _mailWriter As Object = _mailWriterContructor.Invoke(New Object() {_fileStream})

            ' Get reflection info for Send() method on MailMessage
            Dim _sendMethod As MethodInfo = New MailMessage().GetType().GetMethod("Send", BindingFlags.Instance Or BindingFlags.NonPublic)


            ' Call method passing in MailWriter

            _sendMethod.Invoke(msg, BindingFlags.Instance Or BindingFlags.NonPublic, Nothing, New Object() {_mailWriter, True, True}, Nothing)

            ' Finally get reflection info for Close() method on our MailWriter
            Dim _closeMethod As MethodInfo = _mailWriter.GetType().GetMethod("Close", BindingFlags.Instance Or BindingFlags.NonPublic)

            ' Call close method
            _closeMethod.Invoke(_mailWriter, BindingFlags.Instance Or BindingFlags.NonPublic, Nothing, New Object() {}, Nothing)
        End Using

    End Sub
End Module


You may observe that you need to create extensions as a module.

Friday, 28 February 2014

TIFF image support in HTML5

Question:

I am working on a requirement to display multi-page tiff image in a web application. We have been exploring the new canvas feature of html5 but find that handling multiple pages in the tiff image using canvas a bit of challenge. Please share some ideas in resolving this which would be a great help. You may direct me to javascript or such other resources that might help addressing this. Thanks in advance.

By: Nilima Jain

Answer: 

Though most browsers support the canvas feature of HTML5, the tiff format is not supported by all browsers, though IE supports it. So, first ensure you have an agreement on the supported browsers for which you are building this feature, or else you would need a plugin / control for handling tiff images. If you are taking the plugin or activex or applet route, then you have the liberty to make use of some of the client libraries and expose methods to interact with javascript calls from the browser and you are done.

Just in case you are fine with the browser support and are looking for using canvas for handling tiff, then try exploring the tiff.js library. Google also has a code library called tiffus. But I have not personally explored these libaries and you may spend some time trying out these whether these are of any help.

The other approach could be to try out the following approach:

Have a server side component with service APIs to be able to split the pages in the tiff image into multiple jpg images of the same resolution. This service API when queried without any specific page number would load the tiff image and have the pages parsed as individual images and would return the binary of the first page as jpg format. Similarly, the API, when queried with a specific page, it would ensure returning the specific page. The challenge with this approach would be that there would be more hits between the client and server. This can however be optimized by handling this process in-memory and thus minimizing the disk i/o on the server. This approach is likely to work with all browsers and the canvas feature can be leveraged as well.

However, if you have a specific browser to work with, for instance Internet Explorer, the best and easy approach would be to build an ActiveX control and wrapping the tiff image handling within it. In this case, the native windows imaging components might just suffice and no need for a third party library to handle the multi-page tiff image. This ActiveX component should expose necessary APIs, which can be invoked by javascript in response to various events at the client side.

There are quite a few third party commercial libraries to meet this need too.

Saturday, 15 June 2013

Image Drag, Drop and Zoom using HTML5 Canvas

Question:

How can I perform drag and drop of images and the zoom in and out of images using HTML5 Canvas element?

By Iyappan Ranganathan

Response:

There are many free Javascript libraries out there to accomplish many drag and drop and other image manipulation features. The one that I have used and satisfied with is KineticJS library. KineticJS exposes events and methods to manage multiple layers, shapes, images, grouping which in combination would help achieve your needs. Specifically the dragstart and dragend events will be the ones you would be interested to implement the drag and drop feature. For zoom in and out you should be using the setScale method.

Check out these links demonstrating the needed features:

HTML5 Canvas KineticJS Scale Animation Tutorial
HTML5 Canvas Drag and Drop an Image

As I said, there are many Javascript libraries and you may pick the one that best suits your needs.

Saturday, 8 June 2013

Image Capture Component for Web Applications

Question:

I am working on a project, which requires acquiring images from scanners in a web browser. I am curious to know if HTML5 / CSS3 in any way help accomplishing this feature or if there are any javascript libraries out there so that the implementation can work across all browsers. Or else, you may suggest various other options available for me to meet this need.

By: Anonymous

Response:

The HTML5 and CSS3 specifications are all about presentation and thus will be able to detect output devices like print, tv, handheld devices, etc, but there isn't any support for input devices like image scanner or camera. Interacting with such devices can however be achieved using javascript and browser specific ActiveX, Browser Plugin or an Applet. There are many third party libraries with varying capabilities and support for a multitude of programming languages, which can be wrapped within an appropriate component and then use Javascript to interact with it.

I have personally used the EZTwain Library for the windows platform by building a Visual Basic based ActiveX component earlier on and later using VB.NET based library and used it within IE successfully. This library however has support for very many programming languages including C++ and Java. Its usage can be extended across various other browsers by wrapping it within an appropriate component. EZ Twain seems to be reasonably priced and comes with royalty free runtime distribution.

If you are a hardcore programmer, this Code Project Article is worth a look, where the author demonstrates building a C++ wrapper using the TWAIN_32.DLL which used to be distributed by Microsoft in earlier versions of Windows. In later versions of Windows (Windows XP SP1 and later), Microsoft has Windows Image Acquisition Automation Layer, which is worth exploring.

ByteScout Scan SDK is another free (FreeBSD License) library for .NET based applications which can also be explored for its extensibility into other environments. Dynamsoft has an image capture suite with pre-built and ready to consume components for various browsers, but carries a hefty price tag though.

The following are few other Tool kits that are worth exploring.

LeadTools Twain SDK
Acusoft's ImageGear for .NET
Victor Image Processing Library

Almost all of the solutions above are targeted at Windows platform. If you are expecting your solution to work on Non windows platform, you should look for similar libraries and build similar wrapper components for the target browser / platform combination.