.NET- Dealing with windows legacy metafiles

by Svetlozar Angelov 2. March 2010 16:45

I have been trying to create .net image objects from IPicture handles. If the image is a bitmap, there is no problem - The Image.FromHbitmap(IntPtr) method works like a charm. There is a bit of a problem if you try to pass a handle to a ‘legacy metafile’. The constructor MetaFile(IntPtr, bool) needs a handle to an enhanced metafile and fails with “Generic error in GDI+” if you pass a handle to a ‘legacy metafile’. You need to convert the legacy metafile to an enhanced metafile and pass its handle. Here comes the API

[DllImport("gdi32")]
extern static int GetMetaFileBitsEx(IntPtr handle, int size, byte[] buff);

[DllImport("gdi32")]
extern static IntPtr SetWinMetaFileBits(int size, byte[] buff, IntPtr handle, IntPtr metafile);

[DllImport("gdi32")]
extern static bool DeleteMetaFile(IntPtr handle);

GetMetaFileBitsEx parameters – handle to a legacy metafile, buffer size and buffer. If the buffer is set to null the function returns the size of the metafile, otherwise extract the metafile into the buffer and returns the size, so we do

int bufferSize = GetMetaFileBitsEx(HBitmap, 0, null);
byte[] buffer = new byte[bufferSize];
int ret = GetMetaFileBitsEx(HBitmap, bufferSize, buffer);

We no longer need the HBitmap handle, so we have to release the memory -

DeleteMetaFile(HBitmap);

We are now ready to create the enhanced metafile -

IntPtr enhancedMeta = SetWinMetaFileBits(bufferSize, buffer, IntPtr.Zero, IntPtr.Zero); 
Metafile enhancedMetaFile = new Metafile(enhancedMeta, true);

Tags:

programming

SQL Row Number

by Svetlozar Angelov 23. February 2010 15:09

While dealing with the web, a lot of questions came out. One of them (an interesting one) is – What is the row number of a specific tuple?  Let’s specify the problem clearly – suppose we have a table with the contacts and we have implemented a paging feature. Here is the select

USE             AdventureWorks

SELECT          FirstName
                , LastName
FROM            Person.Contact
ORDER BY        LastName

But if we want to navigate to the page where we can find Isabella Washington? we have to find out what is her position in our result set and we just calculate the page –> her row number / N(number of tuples on a page). Here is how we can do it

SELECT          a.RowNo
FROM            (SELECT      ROW_NUMBER() OVER(ORDER BY LastName) AS RowNo
                            , FirstName
                            , LastName
                FROM        Person.Contact ) AS a    
WHERE           a.FirstName = 'Isabella'
                AND a.LastName = 'Washington'
ROW_NUMBER() returns the sequential number of a row within a partition of a result set, starting at 1 for the first row in each partition.

Tags:

programming

C++ Unnamed namespace

by Svetlozar Angelov 17. February 2010 16:08

C++ added the namespace support in order to help you keep your global namespace clean. One of the cool features is the unnamed namespace  . You can hide types from other compilation units. It comes really useful for some specific compilation unit helpers. 

#include <iostream>
namespace 
{
    void Helper()
    {
        std::cout << "I'm a helper function" << std::endl;
    }
}

int main()
{
    Helper();
}

If you have two cpp files you can have only one void Helper() function (not residing in a namespace), because otherwise it would break the one definition rule. With unnamed namespaces you can write all your helper functions without worrying about polluting the global namespace, because they are only visible to the compilation unit (file).

Tags:

programming

IlMerge – Merge .NET Assemblies

by Svetlozar Angelov 2. February 2010 11:06

One of the reasons why I like the .NET framework is the huge amount of third party libraries in the .NET world. However, adding references to dll class libraries leads to several assemblies into your bin directory when sometimes it is more suitable to have just one exe.

Here comes the ILMerge utility. It is a tool for merging multiple .NET assemblies into a single .NET assembly.   

Here is an example how you cam merge an exe and a class library dll into one exe.

lmerge.exe /out:C:\SomePath\TheOnlyOneExe.exe 
                C:\....\bin\Debug\someexe.exe C:\....\bin\Debug\somedll.dll /t:exe

It is important to place your primary assembly (the exe in this case) as the first assembly added in order to keep the entry point. /out: stands for output file and /t: stands for target. 

Tags:

programming

ASP.NET MVC – Pdf View

by Svetlozar Angelov 1. February 2010 11:49

Here is an example how you can return a pdf response in a asp.net mvc project. First of all you need a FileStreamResultmethod in your controller(it handles the pdf request).

public FileStreamResult PdfViewDemo()
{
    Stream Pdf = //code that returns pdf stream
    HttpContext.Response.AddHeader("content-disposition", "attachment; filename=pdfexample.pdf");
    return new FileStreamResult(Pdf, "application/pdf"); 
}  

I’m going to implement a simple pdf load from the hard drive, but the more powerful approach is to generate your pdf “on the fly” using library such as ITextSharp.

public class ReportsHelper
{
    public Stream GeneratePdf()
    {
        FileStream inputPdf = File.OpenRead("C:\\YourPath\SomePdf.pdf");
        MemoryStream ms = new MemoryStream();
        ms.SetLength(inputPdf.Length);
        inputPdf.Read(ms.GetBuffer(), 0, (int)inputPdf.Length);
        return ms;
    }
}   //end ReportsHelper class

and the FileStreamResult method becomes

public FileStreamResult PdfViewDemo() 
{
    ReportsHelper repHelper = new ReportsHelper();
    Stream Pdf = repHelper.GeneratePdf();
    HttpContext.Response.AddHeader("content-disposition", "attachment; filename=pdfexample.pdf");
    return new FileStreamResult(Pdf, "application/pdf"); 
}   

Tags: ,

programming

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen