15 Mayıs 2015 Cuma

c# Taskbar gizleme kodu

c# ile alt barı gizleyebilirsiniz.Taskbar gizleme hide işlemi için kodlar...
using System; using System.Runtime.InteropServices; public static class Taskbar { [DllImport("user32.dll")] private static extern int FindWindow(string className, string windowText); [DllImport("user32.dll")] private static extern int ShowWindow(int hwnd, int command); [DllImport("user32.dll")] public static extern int FindWindowEx(int parentHandle, int childAfter, string className, int windowTitle); [DllImport("user32.dll")] private static extern int GetDesktopWindow(); private const int SW_HIDE = 0; private const int SW_SHOW = 1; protected static int Handle { get { return FindWindow("Shell_TrayWnd", ""); } } protected static int HandleOfStartButton { get { int handleOfDesktop = GetDesktopWindow(); int handleOfStartButton = FindWindowEx(handleOfDesktop, 0, "button", 0); return handleOfStartButton; } } public static void Show() { ShowWindow(Handle, SW_SHOW); ShowWindow(HandleOfStartButton, SW_SHOW); } public static void Hide() { ShowWindow(Handle, SW_HIDE); ShowWindow(HandleOfStartButton, SW_HIDE); } }

10 Mayıs 2015 Pazar

C# dizin kopyalama işlemi

  string sourcePath = @"C:\Users\Public\TestFolder";
        string targetPath =  @"C:\Users\Public\TestFolder\SubDir";
if (System.IO.Directory.Exists(sourcePath))
        {
            string[] files = System.IO.Directory.GetFiles(sourcePath);

            foreach (string s in files)
            {
                fileName = System.IO.Path.GetFileName(s);
                destFile = System.IO.Path.Combine(targetPath, fileName);
                System.IO.File.Copy(s, destFile, true);
            }
        }

C# açık olan dosyaları kopyalama

using(var inputFile = new FileStream(
         "oldFile.txt", 
         FileMode.Open, 
         FileAccess.Read, 
         FileShare.ReadWrite))
     {
        using (var outputFile = new FileStream("newFile.txt", FileMode.Create))
        { 
            var buffer = new byte[0x10000];
            int bytes;

            while ((bytes = inputFile.Read(buffer, 0, buffer.Length)) > 0) 
            {
                outputFile.Write(buffer, 0, bytes);
            }
        }
    }

24 Şubat 2015 Salı

c# hızlı resim işleme

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
private bool CompareBitmaps(Image left, Image right)
{
    if (object.Equals(left, right))
        return true;
    if (left == null || right == null)
        return false;
    if (!left.Size.Equals(right.Size) || !left.PixelFormat.Equals(right.PixelFormat))
        return false;
 
    Bitmap leftBitmap = left as Bitmap;
    Bitmap rightBitmap = right as Bitmap;
    if (leftBitmap == null || rightBitmap == null)
        return true;
 
    #region Code taking more time for comparison
 
    for (int col = 0; col < left.Width; col++)
    {
        for (int row = 0; row < left.Height; row++)
        {
            if (!leftBitmap.GetPixel(col, row).Equals(rightBitmap.GetPixel(col, row)))
                return false;
       }
    }
 
    #endregion
     
    return true;
}
1.5MB resim 2.5 saniye


 40 ms  Bitmap.LockBits() and Bitmap.UnlockBits() 


?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
private bool CompareBitmaps(Image left, Image right)
{
    if (object.Equals(left, right))
        return true;
    if (left == null || right == null)
        return false;
    if (!left.Size.Equals(right.Size) || !left.PixelFormat.Equals(right.PixelFormat))
        return false;
 
    Bitmap leftBitmap = left as Bitmap;
    Bitmap rightBitmap = right as Bitmap;
    if (leftBitmap == null || rightBitmap == null)
        return true;
 
    #region Optimized code for performance
 
    int bytes = left.Width * left.Height * (Image.GetPixelFormatSize(left.PixelFormat) / 8);
 
    bool result = true;
    byte[] b1bytes = new byte[bytes];
    byte[] b2bytes = new byte[bytes];
 
    BitmapData bmd1 = leftBitmap.LockBits(new Rectangle(0, 0, leftBitmap.Width - 1, leftBitmap.Height - 1), ImageLockMode.ReadOnly, leftBitmap.PixelFormat);
    BitmapData bmd2 = rightBitmap.LockBits(new Rectangle(0, 0, rightBitmap.Width - 1, rightBitmap.Height - 1), ImageLockMode.ReadOnly, rightBitmap.PixelFormat);
 
    Marshal.Copy(bmd1.Scan0, b1bytes, 0, bytes);
    Marshal.Copy(bmd2.Scan0, b2bytes, 0, bytes);
 
    for (int n = 0; n <= bytes - 1; n++)
    {
        if (b1bytes[n] != b2bytes[n])
        {
            result = false;
            break;
        }
    }
 
    leftBitmap.UnlockBits(bmd1);
    rightBitmap.UnlockBits(bmd2);
 
    #endregion
 
    return result;
}