An image in a computer consists of three main color channels, they are RED, GREEN, and BLUE. So each pixle in an image has three values one for each channel ranging from 0 up 255. In order to get the gray scale of an image one channel should be used instead, and each pixle then will have only one value called the Gray level. To obtain the Gray level for a pixle of an image, we should use the following formula: Gray = 0.3 * RED + 0.6 * GREEN + 0.1 * BLUE This method applies the previous formula on the pixels of a given image resulting the gray level of the image. public Image ToGrayScale(Image im)
{
int R, G, B, Gray;
// create a Bitmap Object to access pixels.
Bitmap Bm = new Bitmap(im);
for (int i = 0; i < Bm.Size.Height; i++)
{
for (int j = 0; j < Bm.Size.Width; j++)
{
// for each pixel get the the channel colors
R = Bm.GetPixel(j, i).R;
G = Bm.GetPixel(j, i).G;
B = Bm.GetPixel(j, i).B;
// Apply the formula
Gray = (int)(0.3 * R) + (int)(0.6 * G) + (int)(0.1 * B);
Bm.SetPixel(j, i, Color.FromArgb(Gray, Gray, Gray));
}
}
return Bm;
}
Hi, I saw a post on the ASP.NET website that requires the following:
Word files with names of particular date are stored in folder at webserver(i.e name of word file should be a date like 2.20.2007) This word file name format is like: 10.20.2007.doc
One background service will run with the such that, file with name of particular date will be sent on the particular date with the email application for example file with name 2.20.2007 will be sent on 2.20.2007
I would like to share you my solution,
First, since this application is intended to work on the background without interacting with the user (Actually the user is just adding files to the folder ), this should involve a Windows Service Project.
We have to check files every day so if the file name is the same as the date of the day, then send it.
to check the file every day we should use a Timer object that accepts a delegate of the function to be repeated in our case the function should check the file name then if accepted send it.
Here is the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Net.Mime;
using System.Net.Mail;
using System.Threading;
namespace WindowsService1
{
public partial class Service1 : ServiceBase
{
TimerCallback SendMail;
public Service1()
{
InitializeComponent();
SendMail = new TimerCallback(Send);
}
protected override void OnStart(string[] args)
{ //check the folder first after 12 hours the every day.
Timer t = new Timer(SendMail);
t.Change(TimeSpan.FromHours (12), TimeSpan.FromDays(1));
} //the function to be called when the timer ticks
public void Send(object arg)
{
string folderName = @"\WordFiles\";
DirectoryInfo di = new DirectoryInfo(folderName);
FileInfo[] fi = di.GetFiles();
//check every file in the folder
for (int i = 0; i < fi.Length; i++)
{
if (CheckFile(fi[i].Name))
{ //if accepted send the file as attatchment
SendFile(fi[i]);
}
}
}
public bool CheckFile(string name)
{// if thr file name is of the format mm.dd.yyyy then it's accepted
if ((Convert.ToInt32(name.Split('.')[0]) == DateTime.Now.Month) && (Convert.ToInt32(name.Split('.')[1]) == DateTime.Now.Day) && (Convert.ToInt32(name.Split('.')[2]) == DateTime.Now.Year))
{
return true;
}
else
return false;
}
public void SendFile(FileInfo fi)
{
MailMessage m = new MailMessage();
Stream sr = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read);
m.Attachments.Add(new Attachment(sr, "WordFile.doc", MediaTypeNames.Application.Octet));
SmtpClient sc = new SmtpClient();
m.From = new MailAddress("developer85@hotmail.com");
m.To.Add("bashar_ge@hotmail.com");
sc.PickupDirectoryLocation = "localhost";
sc.Send(m);
}
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your service.
}
}
Hi, in this post I’ll show how to create thumbnails of an image while maintaining the same scale of the original image.
//the Path should be of the form “~/Folder/image.gif
//tH and tW are the size of new thumbnail to be generated.
public Image ThumbImage(String Path, int tH,int tW)
{
System.Drawing.Image im=null;
Try
{
im = System.Drawing.Image.FromFile(Server.MapPath(Path));
}
catch (Exception ex) { }
//get the image size
int h = im.Height;
int w = im.Width;
//the new thumbnaial dimensions
Double nh = 0;
Double nw = 0;
//if the original width is greater than the wanted thumbnail width
if (w > tW)
{
//then set the new width to the wanted thumbnail width
nw = tW;
//and also multiply the original image height with the scale ration
nh = h * (nw / w);
}
//Now the same for the Height
//if the original Height is greater than the wanted thumbnail Height
if (nh > tH)
{
//then set the new Height to the wanted thumbnail Height
nh = tH;
//and also multiply the original image width with the scale ration
nw = w * (nh / h);
}
//so if one dimension gets smaller the other will get reduced the same
Image NewIm = new Image();
NewIm.Height = Unit.Pixel((int)nh);
NewIm.Width = Unit.Pixel((int)nw);
NewIm.ImageUrl = Path;
return NewIm;
}
By Default, we can't upload files with size more the 4Mb, because it's already configured in the machine.web file. as follows:
<httpRuntime maxRequestLength ="4096"/> where 4096 in kb
Although you can overload these settings in the web.config file
add in the <system.web>
<httpRuntime maxRequestLength ="5120"/>
Which allows uploading files up to for example 5120 = 5Mb.
In this post I’ll show how to upload files to a server directory, and discuss several used techniques step by step.
In ASP.NET, files can be uploaded many ways. One way is to use the FileUpload control because it’s easy and ready to use.
Some issues should be considered:
1. The types of the file to be uploaded; images (jpg, gif, png) or sound files (wav, rm, mp3…).
2. Where to save the files; copy the file to the server’s file system and save the file path in a database table or we can store the files directly in the database as bytes.
3. And some more else as I go through details.
Add FileUpload control, a Button and a Label to the web page
Starting with file extensions:
First, only allowed file extensions are stored to an array of strings. In case of uploading image files, the common image file extensions are: .jpg, .gif, and .png etc...
You can add whatever you want
string[] ImageExtentions ={ ".jpeg", ".gif", ".png", ".jpg" };
//Get the file extension of the file in the FileUpload control
string FileExtention = Path.GetExtension(FileUpload1.FileName).ToLower();
If the “Browse” button of the FileUpload was pressed and a file was chosen then the Boolean HasFile Property of the FileUpload is set to true. So we proceed only if the HasFile Property is true.
if (FileUpload1.HasFile)
//Test whether the file is of the allowed extensions or not using a simple loop.
bool IsReady = false;
for (int i = 0; i < AllowedExtentions.Length; i++)
{
if (FileExtention == AllowedExtentions[i])
{
IsReady = true;
Break;
}
}
Finally to save the file to the server, it’s easy with a single line of code:
//~represents the root folder
const string Directory = "~/Images/";
FileUpload1.SaveAs(Server.MapPath(Directory + FileUpload1.FileName));
Then the file path should be stored in the datadbase to be retrived later.
But what if the file name is already exist in the server folder, the previous instruction will overwrite the file, so we may face a problem of loosing files of the same names but different contents. To solve this problem two different solutions can be used:
We can simply change the name as follows:
string FilePath = Directory + FileUpload1.FileName;
string FileName = Path.GetFileNameWithoutExtension(FileUpload1.FileName);
string fileExtention = Path.GetExtension(FileUpload1.FileName);
int iteration = 1;
while (File.Exists(Server.MapPath(FilePath)))
{
FilePath = string.Concat(Directory, FileName, "", iteration, fileExtention);
iteration++;
}
try
{
FileUpload1.SaveAs(Server.MapPath(FilePath));
}
catch (Exception ex)
{ UpFilelbl.Text = "Couldn't upload file, because " + ex.Message;
}
For example if the file name is apple.gif and the file is already exist this will try the name apple1.gif, apple2.gif and so on.
Or we could get a random generated file name
string temp=Path.GetRandomFileName();
string FileName = temp.Remove(temp.LastIndexOf('.'));
string fileExtention = Path.GetExtension(FileUpload1.FileName);
string FilePath = Directory+ FileName+ fileExtention;
try
{
FileUpload1.SaveAs(Server.MapPath(FilePath));
}
catch (Exception ex)
{
UpFilelbl.Text = "Couldn't upload file, because " + ex.Message;
}
UpFilelbl.Text = "Couldn't upload file, because " + ex.Message;
}
For example if the file name is apple.gif and the file is already exist this will try the name apple1.gif, apple2.gif and so on.
Or we could get a random generated file name
string temp=Path.GetRandomFileName();
string FileName = temp.Remove(temp.LastIndexOf('.'));
string fileExtention = Path.GetExtension(FileUpload1.FileName);
string FilePath = Directory+ FileName+ fileExtention;
try
{
FileUpload1.SaveAs(Server.MapPath(FilePath));
}
catch (Exception ex)
{
UpFilelbl.Text = "Couldn't upload file, because " + ex.Message;
}