HI Guys ,
Thanks for your quick response for my previous posts ...I need your help again
I am working with a web application in c#..I need to increase and decrease the color contrast of an Image using the scroll bar..please post the code as soon as possible
Loading
Balaji EssakkiPosted Mar 9, 2010, 1:57 AM
Raaj KumarPosted Mar 4, 2010, 4:41 AM
It seems there is no problem in code. Could you please check the permission of the folder for read/write operation if you have hosted in IIS. Otherwise Right-Click on MembershipImages folder and uncheck the readonly property. Since if you dont have permission it will throw this error.
Regards,
raaj
Balaji EssakkiPosted Mar 4, 2010, 4:05 AM
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
public partial class slider : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Image1.ImageUrl = "MembershipImages/Sample.jpg";
}
protected void txtBrightness_TextChanged(object sender, EventArgs e)
{
string filename = Server.MapPath("MembershipImages/brightImage.jpg");
AdjustBrightness(filename, Convert.ToInt16(txtBrightness.Text));
}
protected void txtContrast_TextChanged(object sender, EventArgs e)
{
string filename = Server.MapPath("MembershipImages/brightImage.jpg");
AdjustContrast(filename, Convert.ToSByte(txtContrast.Text));
}
#region Core
public void AdjustBrightness(string fileName, int Value)
{
Bitmap oBitmap = default(Bitmap);
oBitmap = new Bitmap(fileName);
Graphics oGraphic = default(Graphics);
Bitmap bmpNew = new Bitmap(oBitmap.Width, oBitmap.Height);
oGraphic = Graphics.FromImage(bmpNew);
float FinalValue = (float)Value / 255.0f;
float[][] FloatColorMatrix ={
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {FinalValue, FinalValue, FinalValue, 1, 1}
};
ColorMatrix NewColorMatrix = new ColorMatrix(FloatColorMatrix);
ImageAttributes Attributes = new ImageAttributes();
Attributes.SetColorMatrix(NewColorMatrix);
oGraphic.DrawImage(oBitmap, new Rectangle(0, 0, bmpNew.Width, bmpNew.Height), 0, 0, oBitmap.Width, oBitmap.Height, GraphicsUnit.Pixel, Attributes);
oBitmap.Dispose();
oBitmap = bmpNew;
oGraphic.Dispose();
oBitmap.Save(fileName, ImageFormat.Jpeg);
oBitmap.Dispose();
}
public void AdjustContrast(string fileName, float Value)
{
Bitmap oBitmap = default(Bitmap);
oBitmap = new Bitmap(fileName);
Graphics oGraphic = default(Graphics);
Bitmap bmpNew = new Bitmap(oBitmap.Width, oBitmap.Height);
oGraphic = Graphics.FromImage(bmpNew);
float FinalValue = (float)Value / 255.0f;
float[][] FloatColorMatrix ={
new float[] {Value, 0f, 0f, 0f, 0f},
new float[] {0f, Value, 0f, 0f, 0f},
new float[] {0f, 0f, Value, 0f, 0},
new float[] {0f, 0f, 0f, 1f, 0f},
new float[] {0.001f, 0.001f, 0.001f, 0f, 1f}
};
ColorMatrix NewColorMatrix = new ColorMatrix(FloatColorMatrix);
ImageAttributes Attributes = new ImageAttributes();
Attributes.SetColorMatrix(NewColorMatrix);
oGraphic.DrawImage(oBitmap, new Rectangle(0, 0, bmpNew.Width, bmpNew.Height), 0, 0, oBitmap.Width, oBitmap.Height, GraphicsUnit.Pixel, Attributes);
oBitmap.Dispose();
oBitmap = bmpNew;
oGraphic.Dispose();
oBitmap.Save(fileName, ImageFormat.Jpeg);
oBitmap.Dispose();
}
public void AdjustContrast(Bitmap Image, float Value, string tofileName)
{
Value = (100.0f + Value) / 100.0f;
Value *= Value;
System.Drawing.Bitmap TempBitmap = Image;
System.Drawing.Bitmap NewBitmap = new System.Drawing.Bitmap(TempBitmap.Width, TempBitmap.Height);
System.Drawing.Graphics NewGraphics = System.Drawing.Graphics.FromImage(NewBitmap);
NewGraphics.DrawImage(TempBitmap, new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), System.Drawing.GraphicsUnit.Pixel);
NewGraphics.Dispose();
for (int x = 0; x < NewBitmap.Width; ++x)
{
for (int y = 0; y < NewBitmap.Height; ++y)
{
Color Pixel = NewBitmap.GetPixel(x, y);
float Red = Pixel.R / 255.0f;
float Green = Pixel.G / 255.0f;
float Blue = Pixel.B / 255.0f;
Red = (((Red - 0.5f) * Value) + 0.5f) * 255.0f;
Green = (((Green - 0.5f) * Value) + 0.5f) * 255.0f;
Blue = (((Blue - 0.5f) * Value) + 0.5f) * 255.0f;
NewBitmap.SetPixel(x, y, Color.FromArgb(Clamp((int)Red, 255, 0), Clamp((int)Green, 255, 0), Clamp((int)Blue, 255, 0)));
}
}
FileInfo imgFile = new FileInfo(tofileName);
NewBitmap.Save(imgFile.OpenWrite(), System.Drawing.Imaging.ImageFormat.Jpeg);
imgFile = null;
NewBitmap.Dispose();
}
public T Clamp
{
T result = value;
if (value.CompareTo(max) > 0)
result = max;
if (value.CompareTo(min) < 0)
result = min;
return result;
}
#endregion
private void ReadWriteStream(string filePath)
{
byte[] buffer;
FileStream fStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
try
{
int length = (int)fStream.Length;
buffer = new byte[length];
int count, sum = 0;
while ((count = fStream.Read(buffer, sum, length - sum)) > 0)
{
sum += count;
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
fStream.Close();
fStream.Dispose();
}
}
}
Raaj KumarPosted Mar 4, 2010, 3:45 AM
For me it is working. Could you please post the code so that I can tell you what exaclty to be done?
Regards,
Raaj
Balaji EssakkiPosted Mar 4, 2010, 2:43 AM
Raaj KumarPosted Mar 4, 2010, 1:26 AM
Check this application. It is because I didn't handle the object disposal properly. Now it is done. Here I have overwritten the existing image with processed image. For your convenience you can store it in separate file.
Regards,
raaj
Balaji EssakkiPosted Mar 3, 2010, 6:28 AM
A generic error occurred in GDI+.
Raaj KumarPosted Mar 3, 2010, 5:59 AM
Balaji EssakkiPosted Mar 3, 2010, 4:25 AM
Raaj KumarPosted Mar 2, 2010, 9:17 AM
You can refer the similar post which I have answered.
http://www.c-sharpcorner.com/Forums/ShowMessages.aspx?ThreadID=79909
But there is no scrollbar instead Ajax SliderControl is used. However you can make use of the logic behind that.
Regards,
raaj