Monday, June 14, 2010

Smart Coding with Visual Studio

Coding is one of the daily tasks that we developers do. Its like brushing our teeth or taking a shower. But how many of us do it smartly. If we want to spend some of our coding time in other activities like blogging or learning new technologies, then we have to be smart while coding.

So the million dollar question is, What do we do to code smartly? Actually, some of us do it, but we hardly recognize them. We do keep track of some typical day to day coding practices and snippets, so that we can pull them out when needed. Or we blog about them, so that if we come across the same problem again, then just pull out the code and implement it again. And in the process we also help others to know about some of our problems and solutions.

Another way that Visual Studio provides is through Code Snippets. The Studio provides some really smart coding practices by default to ease our coding woes. We can also write our custom snippets, so that we can use them when necessary. But, we hardly do that.

While I was checking some of the cool stuffs in CodePlex, I came across All-In-One Code Framework. This is a bunch of predefined very useful snippets that we can use in our day to day coding. It provides snippets on a wide variety of Technologies starting from COM, IPC, ADO, LINQ, Entity Framework, XML and File Handling to VSTO, WinForms, WPF, ASP.NET, Silverlight, Azure and many more. It provides snippets in C#, VB and C++. Isn't that cool!

The main advantage is that, it takes out the pain of creating snippets on our own. Just install it, and we are ready to go. And more over the broad spectrum of implementation is definitely a deal to die for.

Tuesday, June 01, 2010

Microsoft Dynamics CRM 4.0 - Online User Handbook

Microsoft Dynamics CRM has become a buzzword now a days and all companies are betting big on it. Even Microsoft is projecting it as an xRM platform to get into all sort of industries and domains.
Many companies are building customized applications on Dynamics CRM to meet the business needs. Here is a link that would help you to learn Microsoft Dynamics CRM 4.0
http://www.redware.com/mscrm/handbook/index.html
It is mainly a Handbook for Users that helps them to know the usage of Dynamics CRM.

Tuesday, April 07, 2009

MethodInvoker: Invoking a control from a worker thread

Invoking a control from a worker thread is common when you're doing multithreading in a windows form. The usual pattern for this goes something like:

private delegate void PrettyMuchUselessDelegate(string text);

private void WorkerMethod() {
  string result;
  // perform some excruciating calculations here to fill the variable 'result'
  UpdateUI(result);
}


private void UpdateUI(string text) {
  if(lblResult.InvokeRequired) {
    lblResult.Invoke(new PrettyMuchUselessDelegate(UpdateUI),new object[] { text }));
  } else {
    lblResult.Text = text;
  }
}

Lots of code to do one simple thing: lblResult.Text = text. But by using tools available to us in the framework and the C# compiler, we can compress this down to:

private void WorkerMethod() {
  string result;
  // perform some excruciating calculations here to fill the variable 'result'
  lblResult.Invoke(new MethodInvoker(delegate { lblResult.Text = result; }));
}


Because we know WorkerMethod is running on a worker thread, we don't bother checking InvokeRequired. We can directly call Invoke on our control, passing a new delegate of type System.Windows.Forms.MethodInvoker. MethodInvoker is a delegate for a method that takes no parameters and returns void. Instead of creating a separate method to invoke, we pass in an anonymous method that sets the text of the label. Using MethodInvoker and anonymous methods helps make the code simpler and more elegant, and what's more important than that?

Thursday, September 25, 2008

Creating PDFs with C# using Ghostscript

Portable Document Format (PDF) is a file format from Adobe that enables a document to be distributed on different systems while preserving the layout. It has become a standard for secured and reliable distribution and exchange of electronic documents around the world. It preserves the fonts, images, graphics, and layout of any source document, regardless of the application and platform used to create it, thus making it cross-platform and cross-browser compatible.

With the increased use of PDF documents as a universal format for sharing documents and managing the paperless office, it has become a part of the commercial applications to be able to convert documents of different format to PDF. In this article we will discuss how we can use Ghostscript to convert various documents into PDF.

Read the complete article here.

Serialization in Database

Serialization is the process of converting an object into a stream of bytes which is easily transferable over the network and storing its current state on any permanent storage media like file or database for later use. De-serialization is the reverse process of Serialization, where the stored object is taken and the original object is recreated.

.NET provides classes through its System.Runtime.Serialization namespaces that can be used for serializing and de-serializing objects.

Read the complete article here.

Working with DBISAM Using Microsoft .NET

DBISAM is a word that may seem new for many of us and many would be astonished to know that it is a database. Like any other database, it has the ability to maintain huge amounts of data. This article aims at providing an overview of DBISAM and the ways we can interact with the database through .NET. Let us first discuss about some of the aspects of DBISAM.


Read the complete article Here.

Thursday, February 28, 2008

Convert your PDF to Images


PDF is one of the most widely used format for distribution of electronic documents. My article on ASP Alliance Creating PDFs with C# using Ghostscript gives a brief idea on conversion of different document formats into PDF.


Ghostscript supports output to a variety of image file formats from pdf and ps files. These formats are called "output devices" in Ghostscript terminology. The ghostscript executible gswin32c.exe can be used to achieve this. The command used for this is as follows:

gswin32c -dSAFER -dBATCH -dNOPAUSE -sDEVICE=jpeg -o out_%d.jpg inputFile.pdf
  • -sDEVICE is used to specify the output device or the driver
  • -dSAFER -dBATCH -dNOPAUSE options suppress interactive prompts and enable some security checks on the file to be run.
  • -o is used to specify the output file name. %d option helps to automatically assign file names as per the page number. So, if a file has 10 pages, 10 files will be created.
The supported devices are as follows:

PNG
png16m : 24-bit RGB color
pnggray : grayscale
png256 : 8-bit color
png16 : 4-bit color
pngmono : black-and-white

JPG
jpeg : Produce color JPEG files.
jpeggray : produce grayscale JPEG files.

TIFF
The color TIFF drivers that produce uncompressed output:
tiffgray : Produces 8-bit gray output.
tiff12nc : Produces 12-bit RGB output (4 bits per component).
tiff24nc : Produces 24-bit RGB output (8 bits per component).
tiff32nc : Produces 32-bit CMYK output (8 bits per component).
tiffsep : The tiffsep device creates multiple output files.

TIFF drivers that produce black-and-white output with different compression modes:
tiffcrle : G3 fax encoding with no EOLs
tiffg3 : G3 fax encoding with EOLs
tiffg32d : 2-D G3 fax encoding
tiffg4 : G4 fax encoding
tifflzw : LZW-compatible (tag = 5) compression
tiffpack : PackBits (tag = 32773) compression

BMP
The BMP drivers produces uncompressed images with the help of the following devices:
bmpmono, bmpgray, bmpsep1, bmpsep8, bmp16, bmp256, bmp16m, bmp32b

We can also implement this in a C# application where this command can be executed with the help of the Ghostscript command line tool. Remember, in order to run this we will need the gswin32c.exe in the Application startup path. Below is a code snippet to achieve this.

CODE SNIPPET:
private string createImage(string inputFile, string outputPath)
{
str = new StringBuilder();
string outputFiles = Path.Combine(outputPath, "out_%d.jpg");
string command = "gswin32c -dSAFER -dBATCH -dNOPAUSE -sDEVICE=jpeg -o \"" + outputFiles + "\" \"" + inputFile + "\"";

try
{
Process pdfProcess = new Process();
StreamReader reader;
StreamWriter writer;

ProcessStartInfo info = new ProcessStartInfo("cmd");
info.WorkingDirectory = System.AppDomain.CurrentDomain.BaseDirectory;

info.CreateNoWindow = true;
info.UseShellExecute = false;
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;

pdfProcess.StartInfo = info;
pdfProcess.Start();
writer = pdfProcess.StandardInput;
reader = pdfProcess.StandardOutput;

writer.AutoFlush = true;
writer.WriteLine(command);
writer.Close();

string ret = reader.ReadToEnd();
}
catch (Exception ex)
{
throw ex;
}

return ret;
}

REFERENCES
http://ghostscript.com/doc/8.54/Use.htm
http://pages.cs.wisc.edu/~ghost/doc/cvs/Devices.htm

DOWNLOAD
You can also download a small windows application from here. This is a C# Windows application that takes in a pdf file and converts it to a number of jpg files.

Saturday, October 28, 2006

Welcome to Techno-Corner

This is a small effort to bring all the technical guys in the world closer. Here we can discuss any technical stuffs related to any platform.

Be it Programming, be it hardware, be it latest Technologies, be it ...

All are welcome to exchange their views here.

BMM