Wednesday, January 20, 2010

Private setter for public getter

Hello Friends,

Now i am showing you some very good features of Net 2.0 over .Net 1.0.
Suppose you want to expose a public property. So for that you have
public getter method but what about its setter method.
from .net 2.0 tou can have a private setter for a public getter too like this.

private int x;
public int X
{
get { return x; }
private set { x = value; }
}

Monday, January 11, 2010

Creating Non-Rectangular Form in C#

Hello Friends,


Now here i am trying some very good stuff on C#.net.
The first in this row is creating non rectangular Form in C#.

The Idea comes when i was created Magnifier as a sub part of my project.
When i created a rectangular magnifier it's ok but when it comes to rounded then
i have to think.

From some book i get these idea (also available in many of GDI tutorials).

The idea behind this is that, GDI provides a new Class called GraphicsPath.
With the help of this class you can draw any closed arc, lines and shapes.

So either override onPaint event of form or Onload event put some code stuff like this.


System.Drawing.Drawing2D.GraphicsPath myShape = new System.Drawing.Drawing2D.GraphicsPath();
myShape.AddEllipse(0, 0, this.Width, this.Height);
this.Region = new System.Drawing.Region(shape);

Note:- You are not getting region property directly in intellisence because it is the
property of control class and here this refers the control.

Now in place of AddEllipse you can add any closed shape for your form.
Hope you enjoy....

Wednesday, October 14, 2009

Single Instance Application in C#

Hello Friends,

Today I am discussing here a very HOT topic i.e. Single Instance in C#.

So friends, There are lots of ways to do so.
Some are very easy as 2-3 lines of code, some are big and complicated.
Just go through all of them.

Methods.
  1. Using Process Name, ID etc.
  2. By using Mutex class
  3. By using Microsoft.VisualBasic.dll
  4. By locking or binding a file.
  5. By locking a particular port.
First Method:- Using Process class

Step1. Give your window form a specific unique name.

this.Text = “My_Application_144”; // title name

Step2. Create a private method for checking all Process title Text.

private bool IsAnotherInstance()
 {
    foreach (Process process in Process.GetProcesses())
     {
        //give particular name same as you given above
        if (process.MainWindowTitle == this.Text) 
           return false;
     }
    return true;
 }

Step3. On form load method check another instance

if (!IsAnotherInstance ())
 {
     MessageBox.Show("Application is Already running.");
     this.Close();
 }

I will disscuss other method next time.
==================================================
2. Using Mutex Class

  
   [STAThread]
   static void Main()
   {
     bool isNew = true;
     using (Mutex mutex = new Mutex(true, "MyApplicationName", out isNew))
     {
       if (isNew)
       {
          Application.EnableVisualStyles();
          Application.SetCompatibleTextRenderingDefault(false);
          Application.Run(new Form1());
       }
       else
       {
          MessageBox.Show("Already running");
       }
     }
   }

Saturday, October 10, 2009

Question Related to .Net Technology.


Hello Friends,

Now i am going to post some of very good question and Article regarding .Net.

Wednesday, April 8, 2009

Convert Colored Images to Black And white in java


Hi,

Sometime you want to convert colored images to black and white.
Here i am giving you the simple method to do so.
Just see below.

//example for coverting colored image to black and white
import java.awt.image.*;
import javax.imageio.ImageIO;
import java.io.*;
import java.awt.*;

class BlackAndWhite
{
          public static void main(String asr[])
          {
                   try
                   {
                             //colored image path
                             BufferedImage image = ImageIO.read(new  
                                                File("D:\\MyProgram\\output.jpg"));

                             //getting width and height of image
                             double image_width = image.getWidth();
                             double image_height = image.getHeight();

                             BufferedImage bimg = null;
                             BufferedImage img = image;

                             //drawing a new image      
                             bimg = new BufferedImage((int)image_width, (int)image_height,
                                                                    BufferedImage.TYPE_BYTE_GRAY);
                             Graphics2D gg = bimg.createGraphics();
                             gg.drawImage(img, 0, 0, img.getWidth(null), img.getHeight(null), null);

                             //saving black and white image onto drive
                             String temp = "blackAndwhite.jpeg";
                             File fi = new File("D:\\My Program\\" + temp);
                             ImageIO.write(bimg, "jpg", fi);
                   }
                   catch (Exception e)
                   {
                             System.out.println(e);
                   }
          }
}