Tuesday, April 21, 2009

ResizeImage From source path to destination path

public void ResizeImage(string sourFilePath, int MaxSideSize, string destFilePath)
{
System.Drawing.Image imgInput = System.Drawing.Image.FromFile(sourFilePath);
int intNewWidth;
int intNewHeight;

//Determine image format
ImageFormat fmtImageFormat = imgInput.RawFormat;

//get image original width and height
int intOldWidth = imgInput.Width;
int intOldHeight = imgInput.Height;

//determine if landscape or portrait
int intMaxSide;

if (intOldWidth >= intOldHeight)
{
intMaxSide = intOldWidth;
}
else
{
intMaxSide = intOldHeight;
}
if (intMaxSide > MaxSideSize)
{
//set new width and height
double dblCoef = MaxSideSize / (double)intMaxSide;
intNewWidth = Convert.ToInt32(dblCoef * intOldWidth);
intNewHeight = Convert.ToInt32(dblCoef * intOldHeight);
}
else
{
intNewWidth = intOldWidth;
intNewHeight = intOldHeight;
}
//create new bitmap
Bitmap bmpResized = new Bitmap(imgInput, intNewWidth, intNewHeight);

bmpResized.Save(destFilePath, ImageFormat.Jpeg);
}

Extract Zip file

using java.util;
using java.util.zip;
using java.io;

private void Extract(string zipFileNamePath, string destinationPath)
{
ZipFile zipfile = new ZipFile(zipFileNamePath);
List zipFiles = GetZipFiles(zipfile);

foreach (ZipEntry zipFile in zipFiles)
{
if (!zipFile.isDirectory())
{
InputStream s = zipfile.getInputStream(zipFile);
try
{
Directory.CreateDirectory(destinationPath + "\\");
FileOutputStream dest = new FileOutputStream(Path.Combine(destinationPath + "\\", Path.GetFileName(zipFile.getName())));
try
{
int len = 0;
sbyte[] buffer = new sbyte[8000];
while ((len = s.read(buffer)) >= 0)
{
dest.write(buffer, 0, len);
}
}
catch (Exception ex)
{
dest.close();
s.close();
}
finally
{
dest.close();
}
dest.close();
}
catch (Exception ex)
{
s.close();
}
finally
{
s.close();
}
s.close();
}
}
zipfile.close();
}

Wednesday, January 7, 2009

Switch ON and OFF the monitor

How to turn off your monitor via code?
This is an important question at least to me , because I use my computer for long time daily and frequently turn on and off the screen as a way to save energy and power .
I hate to make that by using the monitor button(off/on) , because I hear sound from the monitor feel me that the screen may damage .
So I will inform you here , How to turn off your monitor via code ( C# ) ?

I will code that depending in API called SendMessage function it’s useful to handle monitor states - the display is going to low power, the display is being shut off and the display is turned on .

Syntax :
LRESULT SendMessage( HWND hWnd, UINT Msg,WPARAM wParam, LPARAM lParam );


Parameters:
  • # HWnd (first paramter)
    Handle to the window whose window procedure will receive the message. If you don't want to bother creating a window to send the message to, you can send the message to all top level windows (HWND_BROADCAST) or you can use GetDesktopWindow function sending the message to the desktop window.
  • # Msg (second paramter)
    Specifies the message to be sent (WM_SYSCOMMAND).
  • # wParam(Third paramter)
    Specifies additional message-specific information (SC_MONITORPOWER).
  • # LParam (last paramter)
    * 1 - the display is going to low power.
    * 2 - the display is being shut off.
    * –1 - the display is being turned on (undocumented value).

Start the coding now :

First of all you should use
using System.Runtime.InteropServices; //to DllImport

public int WM_SYSCOMMAND = 0x0112;
public int SC_MONITORPOWER = 0xF170; //Using the system pre-defined MSDN constants that can be used by the SendMessage() function .


[DllImport("user32.dll")]
private static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);
//To call a DLL function from C#, you must provide this declaration .


private void button1_Click(object sender, System.EventArgs e)
{

SendMessage( this.Handle.ToInt32() , WM_SYSCOMMAND , SC_MONITORPOWER ,2 );//DLL function
}

Ref:- http://fci-h.blogspot.com/2007/03/turn-off-your-monitor-via-code-c.html

Thursday, November 6, 2008

Resize image

private System.Drawing.Image ResizeToMaxSize(System.Drawing.Image image, int maxWidth, int maxHeight, float xDpi, float yDpi)
{

if (image == null)
{
return image;
}
int width = image.Width;
int height = image.Height;

double widthFactor = (Convert.ToDouble(width) / Convert.ToDouble(maxWidth));
double heightFactor = (Convert.ToDouble(height) / Convert.ToDouble(maxHeight));

if (widthFactor < 1 && heightFactor < 1)
{
// Skip resize
return image;
}
else
{
int newWidth;
int newHeight;
if (widthFactor > heightFactor)
{
newWidth = Convert.ToInt32(Convert.ToDouble(width) / widthFactor);
newHeight = Convert.ToInt32(Convert.ToDouble(height) / widthFactor);
}
else
{
newWidth = Convert.ToInt32(Convert.ToDouble(width) / heightFactor);
newHeight = Convert.ToInt32(Convert.ToDouble(height) / heightFactor);
}

Bitmap bitmap = new Bitmap(newWidth, newHeight, PixelFormat.Format24bppRgb);
bitmap.SetResolution(xDpi, yDpi);

Graphics graphics = Graphics.FromImage(bitmap);
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.DrawImage(image, 0, 0, newWidth, newHeight);
image.Dispose();
return bitmap;
}
}

Friday, September 26, 2008

Upgrade varbinary(max) to varbinary(max) FILESTREAM

Code Snippet

-- Initial schema:

CREATE TABLE [dbo].[Pictureimage](

[PictureId] [uniqueidentifier] NOT NULL,

[Image] [varbinary](max) NOT NULL,

[OriginalImage] [varbinary](max) NOT NULL,

[Version] [timestamp] NOT NULL,

CONSTRAINT [PK_Pictureimage] PRIMARY KEY CLUSTERED

(

[PictureId] ASC

)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]

-- Attach 90 db…

-- Set Comp level

EXEC sp_dbcmptlevel Amigo, 100;

GO

-- Create FILESTREAM filegroup

ALTER database Amigo

ADD FILEGROUP fsfg_Amigo

CONTAINS FILESTREAM

GO

--Add a file for storing database photos to FILEGROUP

ALTER database Amigo

ADD FILE

(

NAME= 'fs_Amigo',

FILENAME = 'C:\fs_Amigo'

)

TO FILEGROUP fsfg_Amigo

GO

-- Migration to FILESTREAM

ALTER TABLE dbo.PictureImage

SET ( FILESTREAM_ON = fsfg_Amigo )

GO

ALTER TABLE dbo.PictureImage

ALTER COLUMN PictureId ADD ROWGUIDCOL

GO

ALTER TABLE dbo.PictureImage

ADD OriginalImageFS varbinary(MAX) FILESTREAM NULL;

GO

UPDATE dbo.PictureImage SET OriginalImageFS = [OriginalImage];

GO

ALTER TABLE dbo.PictureImage

DROP COLUMN OriginalImage;

GO

EXEC sp_rename 'AmigoProd.dbo.PictureImage.OriginalImageFS', 'OriginalImage', 'COLUMN';

GO

Monday, September 15, 2008

Recive PostSubmitter

if (Request.HttpMethod == "POST")
{
Stream dataStream = Request.InputStream;
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
string[] splittempQuery = responseFromServer.Split(("&").ToCharArray());
for (int i = 0; i < splittempQuery.Length; i++)
{
string[] qryValues = splittempQuery[i].Split(("=").ToCharArray());
if (qryValues[0].ToLower() == "op")
{
string aa = qryValues[1].ToString();
}
else if (qryValues[0].ToLower() == "rel_code")
{
string aa = qryValues[1].ToString();
}
else if (qryValues[0].ToLower() == "FREE_TEXT")
{
string aa = qryValues[1].ToString();
}
else if (qryValues[0].ToLower() == "SEARCH")
{
string aa = qryValues[1].ToString();
}
}
reader.Close();
dataStream.Close();
}

call postsubmitter

PostSubmitter post = new PostSubmitter();
post.Url = url;
post.PostItems.Add("op", "100");
post.PostItems.Add("rel_code", "1102");
post.PostItems.Add("FREE_TEXT", "c# jobs");
post.PostItems.Add("SEARCH", "");
post.Type = PostSubmitter.PostTypeEnum.Post;
string result = post.Post();