30 Mayıs 2011 Pazartesi

Convert to Unix Epoch Time

private string ConvertUnixEpochTime(long epoch)
{
    try
{
long baseTicks = 621355968000000000;
long tickResolution = 10000000;
long epochNegative = 1000000000;
long epochTicks = (epoch - epochNegative * tickResolution) + baseTicks; string date = new DateTime(epochTicks, DateTimeKind.Utc).ToString();
return date;
}
catch (Exception exp)
{
}
return string.Empty;
}

Kullanımı :
private void button1_Click(object sender, EventArgs e)
{
txtMesaj.Text = ConvertUnixEpochTime(epoch format);
}

9 Mayıs 2011 Pazartesi

DeflateStream ile Compress ve Decompress

Byte türündeki dataları sıkıştırmaya yarayan DeflateStream ile yazılan Compress ve Decompress Methodları...

using System.IO;
using System.IO.Compression;
using System;

public static class Deflate
    {
        public static byte[] Compress(byte[] Bytes)
        {
            if (Bytes == null)
                throw new ArgumentNullException("Bytes");
            using (MemoryStream Stream = new MemoryStream())
            {
                using (DeflateStream ZipStream = new DeflateStream(Stream, CompressionMode.Compress, true))
                {
                    ZipStream.Write(Bytes, 0, Bytes.Length);
                    ZipStream.Close();
                    return Stream.ToArray();
                }
            }
        }

        public static byte[] Decompress(byte[] Bytes)
        {
            if (Bytes == null)
                throw new ArgumentNullException("Bytes");
            using (MemoryStream Stream = new MemoryStream())
            {
                using (DeflateStream ZipStream = new DeflateStream(new MemoryStream(Bytes), CompressionMode.Decompress, true))
                {
                    byte[] Buffer = new byte[4096];
                    while (true)
                    {
                        int Size = ZipStream.Read(Buffer, 0, Buffer.Length);
                        if (Size > 0) Stream.Write(Buffer, 0, Size);
                        else break;
                    }
                    ZipStream.Close();
                    return Stream.ToArray();
                }
            }
        }

6 Mayıs 2011 Cuma

Verman Encryption-Decryption

One-time pad belirli usullerle karıştırılmış harflerden oluşturulan tek kullanımlık şifreleme yöntemidir. Bir diğer adı ise Vernam şifreleme yöntemidir.

    public static class VernamEncryption
    {
         public static string Encrypt(string Input, string Key)
        {
            return Process(Input, Key);
        }
        public static string Decrypt(string Input, string Key)
        {
            return Process(Input, Key);
        }
        private static string Process(string Input, string Key)
        {
            if (string.IsNullOrEmpty(Input))
                throw new ArgumentNullException("Input");
            if (string.IsNullOrEmpty(Key))
                throw new ArgumentNullException("Key");
            if (Input.Length != Key.Length)
            {
                throw new ArgumentException("Key is not the same length as the input string");
            }
            ASCIIEncoding Encoding = new ASCIIEncoding();
            byte[] InputArray = Encoding.GetBytes(Input);
            byte[] KeyArray = Encoding.GetBytes(Key);
            byte[] OutputArray = new byte[InputArray.Length];
            for (int x = 0; x < InputArray.Length; ++x)
            {
                OutputArray[x] = (byte)(InputArray[x] ^ Key[x]);
            }
            return Encoding.GetString(OutputArray);
        }
    }

Screenshot Class

Projelerinizde kullanabileceğiniz klavyedeki PrintScreen tuşunun görevini yapan class örneği...

using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System;

 public static class Screenshot
    {
        public static void TakeScreenShot(string FileName)
        {
            if (string.IsNullOrEmpty(FileName))
                throw new ArgumentNullException("FileName");
            using (Bitmap TempBitmap = Screenshot.TakeScreenShot())
            {
                TempBitmap.Save(FileName);
            }
        }

        public static Bitmap TakeScreenShot()
        {
            Rectangle TotalScreenRect = Rectangle.Empty;
            foreach (Screen CurrentScreen in Screen.AllScreens)
            {
                TotalScreenRect = Rectangle.Union(TotalScreenRect, CurrentScreen.Bounds);
            }
            Bitmap TempBitmap = new Bitmap(TotalScreenRect.Width, TotalScreenRect.Height, PixelFormat.Format32bppArgb);
            using (Graphics TempGraphics = Graphics.FromImage(TempBitmap))
            {
                TempGraphics.CopyFromScreen(TotalScreenRect.X, TotalScreenRect.Y, 0, 0, TotalScreenRect.Size, CopyPixelOperation.SourceCopy);
            }
            return TempBitmap;
        }
    }

2 Mayıs 2011 Pazartesi

ItextSharp ConcetenatePDF

Var olan bir pdf deki verileri yeniden oluşturacağımız bir pdf e kopyalamak için yazmış olduğum bir method iyi günlerde kullanmanız dileğiyle...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTextSharp.text.pdf;
using iTextSharp.text;

        public void ConcetenatePDF(String sourcePdf, String[] destinationPdf)
        {
            Document doc = new Document();
            PdfCopy copy = new PdfCopy(doc, new FileStream(sourcePdf, FileMode.Create));
            doc.Open();
            PdfReader reader;
            int n;

            for (int i = 0; i < destinationPdf.Length; i++)
            {
                reader = new PdfReader(destinationPdf[i]);
                n = reader.NumberOfPages;

                for (int page = 0; page < n; )
                {
                    copy.AddPage(copy.GetImportedPage(reader, ++page));
                }
                copy.FreeReader(reader);
            }
            doc.Close();
        }