Saturday, April 24, 2010

Resize Image High Quality C#.Net with Size Ratio and File Upload Control

public bool SaveThumbnailImageHQ(System.Web.UI.WebControls.FileUpload fu, string FullSavePath, int HeightMax, int WidthMax)
{
    try
    {
        if (fu.HasFile)
        {
            Size newSize = new Size(WidthMax, HeightMax);
            using (Bitmap bmp = new Bitmap(fu.PostedFile.InputStream))
            {
                double ratioWidth = (double)bmp.Size.Width / (double)newSize.Width;
                double ratioHeight = (double)bmp.Size.Height / (double)newSize.Height;
                double ratio = Math.Max(ratioHeight, ratioWidth);
                int newWidth = (int)(bmp.Size.Width / ratio);
                int newHeight = (int)(bmp.Size.Height / ratio);
                newSize = new Size(newWidth, newHeight);
                using (Bitmap thumb = new Bitmap((System.Drawing.Image)bmp, newSize))
                {
                    using (Graphics g = Graphics.FromImage(thumb)) // Create Graphics object from original Image
                    {
                        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                        g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                        System.Drawing.Imaging.ImageCodecInfo codec = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()[1];
                        System.Drawing.Imaging.EncoderParameters eParams = new System.Drawing.Imaging.EncoderParameters(1);
                        eParams.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
                        g.DrawImage(bmp, new Rectangle(0, 0, thumb.Width, thumb.Height));
                        if (System.IO.File.Exists(FullSavePath))
                        {
                            try { System.IO.File.Delete(FullSavePath); }
                            catch { }
                        }
                        thumb.Save(FullSavePath, codec, eParams);
                        //setError(false, string.Empty);
                        return true;
                    }
                }
            }
        }
        else
        {
            //setError(true, "No file input found.");
            return false;
        }
    }
    catch (Exception ex)
    {
        //setError(true, ex.Message);
        return false;
    }
}

Monday, April 19, 2010

All Font Preview Microsoft Word Macro

Sub Generate_Font_Preview()
Dim doc As Word.Document
Dim objRange As Range
Dim oTable  As Word.Table
Dim iCnt  As Integer
Dim s1 As Range
Dim str As String

Set doc = Application.Documents.Add
Set objRange = doc.Range()
str = InputBox("Please enter Preview Text", "Preview Text")

doc.Tables.Add objRange, Application.FontNames.Count, 2
Set oTable = doc.Tables(1)


For iCnt = 1 To FontNames.Count
    If (IsEmpty(str)) Then
        str = Application.FontNames(iCnt)
    End If
    oTable.Cell(iCnt, 1).Range.Text = Application.FontNames(iCnt)
    oTable.Cell(iCnt, 1).SetWidth ColumnWidth:=InchesToPoints(1.5), RulerStyle:=wdAdjustSameWidth
    With oTable.Cell(iCnt, 2).Range
        .Text = str
        .Font.Name = Application.FontNames(iCnt)
        .Font.Size = 25
        .Font.Color = WdColor.wdColorBlack
    End With
Next iCnt
End Sub

Wednesday, April 7, 2010

Split string-value with delimeter in Sql Server via XML

Declare @xml as xml
declare @str as varchar(100)
declare @delimiter as varchar(10)
SET @str=',A,B,C,D,E,,,'
SET @delimiter =','
SET @xml = cast((''+replace(@str,@delimiter ,'')+'') as xml)
select @xml
SELECT N.value('.', 'varchar(max)') as value 
FROM @xml.nodes('x') as T(N) where N.value('.', 'varchar(max)')<>''