Monday, December 7, 2009

How to generate a transparent GIF image using C#

There is apparently no easy way to generate a transparent GIF image using the .NET framework. Microsoft provided a method in the Bitmap class called MakeTransparent() but it doesn’t work for GIFs, it only seems to work for PNGs.

To create a transparent GIF you need to recreate the color table of the image using Imaging APIs. Unfortunately, this can be pretty slow for an ASP.NET Web application, and it has a lot of overhead, so I needed an alternative.

Solution:

The alternative is to alter the color table directly using the GIF specification. To implement this, I needed to save the non-transparent GIF to a MemoryStream, and then go through the binary data, updating the color table with the new transparency bits.

Wednesday, October 28, 2009

How to increase sound level using C#

To increase sound level using C# first of all you should add line:

using System.Runtime.InteropServices;

to allow using COM Interop in your class.

Next we derlare two functions from an unmanaged DLL:

Friday, October 23, 2009

Capture Sound and save into wav or mp3 file .NET C# example

It's surprising that there are no components for sound capturing in .NET Framework 3.5. Even designers of WPF and Silverlight 2.0 were focused on graphics so deeply, that they forgot about applications recording sound from user's microphone. It is said that the next version of Silverlight will provide such functionality.

However, what you often want to achieve is to store the recorded sound in MP3 file (or send it as MP3 stream). That's legally complicated due to MP3 patent constraints. And for the same legal reason, we can assume that we will not see MP3 functionality in Microsoft technologies soon (there is WMA instead).

Wednesday, July 29, 2009

How to get data from Firebird database using MS SQL Server stored procedure

In solving one of the tasks (рассчет автокредита) I needed an opportunity to perform stored procedure of SQL Server 2005, to receive data from the FireBird database.

Typically stored procedure in T-SQL can not do this, so I wrote a stored procedure in C#, which receives the necessary data to the DataTable(s), from where then method SqlContext.Pipe.SendResultsRow, passing a string SQL Server.
Here is the example:

Wednesday, May 6, 2009

How to connect to SQL Server 2008 using 2005

While supporting the website калькулятор на автокредит I encountered a problem when I use SQL Server Management Studio in SQL Server 2005 to connect to an instance of SQL Server 2008.
To solve this problem, I installed Microsoft SQL Server 2005 Service Pack 2. After that I also installed "Cumulative update package 12 for SQL Server 2005 Service Pack 2 (server and client)".
And, voila, it works!

Tuesday, April 28, 2009

How to sort the hierarchical recursive query in SQL Server 2005

In SQL Server 2005 was an innovation in the form of CTE (common table expressions), which enables the hierarchical recursive queries to the database. While designing the web site автокредит на автомобиль I have a challenge: to display the hierarchical structure of pages of the site. The query also have to sort sort the data on a field "pageorder" from the pages with the same level of hierarchy.

The structure of the table looks like this:


CREATE TABLE [dbo].[pages](
[id] [int] NULL,
[pid] [int] NULL,
[title] [nvarchar](max) NULL,
[pageorder] [int] NULL
) ON [PRIMARY]


The field "pid" points to id of the parent record. In addition, there have to exist a root(entry) record, whose pid = NULL.

Below is a sample request that selects and sorts the hierarchical data:

Wednesday, March 25, 2009

How to disable passing of HTTP header "Expect: 100-continue" using HttpWebRequest in .NET Framework in C#?

How to disable passing of HTTP header "Expect: 100-continue" using HttpWebRequest in .NET Framework in C#?
По умолчанию, при выполнении веб-запроса, используя класс HttpWebRequest, .NET Framework добавляет HTTP заголовок (HTTP header) "Expect: 100-continue". Для того, чтобы запретить формирование этого заголовка нужно сделать следующее:

As default when You use HttpWebRequest class to execute web-requests it appends HTTP header "Expect: 100-continue" to it's headers collection. To disable passing of HTTP header "Expect: 100-continue" using HttpWebRequest You can do following:

How to place DateTimePicker onto ToolStrip control in Windows Forms .NET application?

To place DateTimePicker onto ToolStrip control in Windows Forms .NET application you had to do following:

DateTimePicker dateTimePcr = new DateTimePicker();
toolStrip1.Items.Add(new ToolStripControlHost(dateTimePcr));

Tuesday, March 3, 2009

How to enabling Gzip and Deflate HTTP Compression in ASP.NET pages

If your ASP.NET web application doesn't already contain a Global.asax file, create a new one using notepad. Then insert following code in it:


<%@ Application Language="C#" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.IO.Compression" %>

Thursday, February 19, 2009

Dotnetnuke: how to force dotnetnuke to send welcome e-mail as HTML not plain text

To force dotnetnuke to send welcome e-mail as HTML, you just have to modify MAIL_USER_REGISTRATION_PUBLIC_BODY.Text or EMAIL_USER_REGISTRATION_PRIVATE_BODY.Text strings in language editor. DNN Framework check if the mailbody contains html code and send e-mail in corresponding mode.

Wednesday, February 18, 2009

How to show name of sender before the e-mail address using ASP.Net MailMessage object

How to show name of sender before the e-mail address using ASP.Net MailMessage object? It's very simple. Just look at following example:


System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.From = new System.Net.Mail.MailAddress("admin of TEST.COM <admin@test.com>");


That code was used in writing website Краш-тесты автомобилей

Thursday, February 5, 2009

How to change in code the title of web page in Dotnetnuke based website?

In the ordinal web page to change programmatically the title of the .aspx page is simple:


protected void Page_Load(object sender, EventArgs e)
{
this.Title = "The ordinal web page";
}


In dotnetnuke's module that code will not help. The title of the web page will be created by DNN's core.

But we can solve this problem. The code below shows how to achieve this:

Tuesday, February 3, 2009

LINQ to SQL: How to get a Random Record from a Table inside the SQL Server Database

I wanted to get a random record from a particular table on the SQL Server database. For my case, I had a table containing quotes, and I wanted to randomly pick on to show on one corner of my web site. I am also using LINQ to SQL for this web site, so the solution here will be using LINQ, although the approach is pretty simple and flexible.

The idea centers on ordering the records by a random parameter an then getting the TOP n number of records. The NEWID() function from SQL Server makes a great candidate for getting a random parameter since this method generates a GUID.

In SQL Server, try out this query.


SELECT * FROM Quote ORDER by NEWID();


SELECT TOP 1 * FROM Quote ORDER by NEWID();

Friday, January 30, 2009

Remember Me not working for Login in DotNetNuke based website

The "Remember Me" feature of the DotNetNuke Login is implemented by placing an encrypted authentication cookie on the user's machine.

The expiration of this cookie is controlled through the timeout value in the Forms Autentication node the web.config.

< forms name=".DOTNETNUKE" protection="All" timeout="60" cookieless="UseCookies" />

The default setting is 60 minutes, so it can't remember anyone for very long (I call it goldfish mode).

To extend the time your users are remembered make the timeout value larger.

Tuesday, January 13, 2009

display:inline-block for Firefox, Internet Explorer and Opera

For correct displaying of the elements with display:inline-block property in Firefox Internet Explorer, Opera you have to write this in style sheet:


display:-moz-inline-stack;/*Firefox need this to simulate display:inline-block*/

display:inline-block; /*IE does not apply this to Block Element, and Firefox does not render this, too*/

_overflow:hidden;/*fix IE6 to expanded content*/
zoom:1;/*trigger hasLayout*/
*display:inline;/*once hasLayout is true, set display:inline to block element will make display:inline behave like display:inline-block*

Tuesday, January 6, 2009

How to reduce the size of the database in Microsoft SQL Server

On hosting server often exists database quota for MS SQL Server. Even if your database contains small amount of data, but there is goes intensive adding/modifacation of the data, in some point the size of the data could exceed quota and your account may be suspended.
This happens because of growth of the transaction log.
To clear transaction log you can execute the following command: