PDA

View Full Version : تبدیل عکس رنگی به سیاه سفید



ramin149
دوشنبه 31 خرداد 1389, 11:09 صبح
جه شکلی می تونم یک عکس با فایل آپلود بگیره و آن رو سیاه سفید کنم و در سایت ذخیره کنم ؟

Mostafa_Dindar
دوشنبه 31 خرداد 1389, 11:25 صبح
جه شکلی می تونم یک عکس با فایل آپلود بگیره و آن رو سیاه سفید کنم و در سایت ذخیره کنم ؟

فکر میکنم با مطالعه یکی از این صفحات جوابتون رو میگیرید :

How to convert a colour image to grayscale (http://www.bobpowell.net/grayscale.htm)

(http://www.dreamincode.net/code/snippet2063.htm) How To Convert Colored Image To Black And White In Asp Net (http://www.dreamincode.net/code/snippet2063.htm) (http://www.dreamincode.net/forums/index.php?app=core&module=global&section=register&coppa_user=&termsread=1&coppa_pass=1&agree_to_terms=1)

Convert colored image into black and white image in VB.net - C#‎.net. (http://www.devasp.net/net/articles/display/1065.html)


سربلند باشید

ramin149
سه شنبه 06 مهر 1389, 16:56 عصر
من کد زیر رو استفاده کردم ولی هنگام ذخیره عکس در داخل فولدر این خطا رو می ده


A generic error occurred in GDI+.

این هم کد نوشته شده در کلاس



string myblack = HttpContext.Current.Server.MapPath(".") + "//photos//black//";
try
{

Bitmap img = new Bitmap(System.Drawing.Image.FromFile(HttpContext.C urrent.Server.MapPath(@"~/photos/admin/" + filename)));

Color c = new Color();

for (int i = 0; i < img.Width; i++)
{

for (int j = 0; j < img.Height; j++)
{

c = img.GetPixel(i, j);

int r = 0;

r = Convert.ToInt16(c.R);

int g = 0;

g = Convert.ToInt16(c.G);

int b = 0;

b = Convert.ToInt16(c.B);

int ans = (r + g + b) / 3;

if (ans > 128)
{

r = 255;

g = 255;

b = 255;

}

else
{

r = 0;

g = 0;

b = 0;

}

c = Color.FromArgb(r, g, b);

img.SetPixel(i, j, c);

}

}

img.Save(myblack);
Error.Text = "SUCESSFULLY DONE";

}

catch (Exception ex) { Error.Text = ex.Message; }

blackcats
سه شنبه 06 مهر 1389, 19:39 عصر
من فکر کنم عکستو پیدا نمیکنه

ramin149
چهارشنبه 07 مهر 1389, 00:42 صبح
من با این کد تونستم عکس رنگی رو به سیاه سفید تبدیل کنم



System.Drawing.Image TempImage = System.Drawing.Image.FromFile(HttpContext.Current. Server.MapPath(@"~/photos/admin/" + filename));
System.Drawing.Imaging.ImageFormat ImageFormat = TempImage.RawFormat;
System.Drawing.Bitmap TempBitmap = new System.Drawing.Bitmap(TempImage, TempImage.Width, TempImage.Height);
System.Drawing.Bitmap NewBitmap = new System.Drawing.Bitmap(TempBitmap.Width, TempBitmap.Height);
System.Drawing.Graphics NewGraphics = System.Drawing.Graphics.FromImage(NewBitmap);
float[][] FloatColorMatrix ={
new float[] {.3f, .3f, .3f, 0, 0},
new float[] {.59f, .59f, .59f, 0, 0},
new float[] {.11f, .11f, .11f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
};
System.Drawing.Imaging.ColorMatrix NewColorMatrix = new System.Drawing.Imaging.ColorMatrix(FloatColorMatri x);
System.Drawing.Imaging.ImageAttributes Attributes = new System.Drawing.Imaging.ImageAttributes();
Attributes.SetColorMatrix(NewColorMatrix);
NewGraphics.DrawImage(TempBitmap, new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), 0, 0, TempBitmap.Width, TempBitmap.Height, System.Drawing.GraphicsUnit.Pixel, Attributes);
NewGraphics.Dispose();
NewBitmap.Save(myblack+filename, System.Drawing.Imaging.ImageFormat.Jpeg);

aliziveie
پنج شنبه 26 اردیبهشت 1392, 12:25 عصر
من با این کد تونستم عکس رنگی رو به سیاه سفید تبدیل کنم



System.Drawing.Image TempImage = System.Drawing.Image.FromFile(HttpContext.Current. Server.MapPath(@"~/photos/admin/" + filename));
System.Drawing.Imaging.ImageFormat ImageFormat = TempImage.RawFormat;
System.Drawing.Bitmap TempBitmap = new System.Drawing.Bitmap(TempImage, TempImage.Width, TempImage.Height);
System.Drawing.Bitmap NewBitmap = new System.Drawing.Bitmap(TempBitmap.Width, TempBitmap.Height);
System.Drawing.Graphics NewGraphics = System.Drawing.Graphics.FromImage(NewBitmap);
float[][] FloatColorMatrix ={
new float[] {.3f, .3f, .3f, 0, 0},
new float[] {.59f, .59f, .59f, 0, 0},
new float[] {.11f, .11f, .11f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
};
System.Drawing.Imaging.ColorMatrix NewColorMatrix = new System.Drawing.Imaging.ColorMatrix(FloatColorMatri x);
System.Drawing.Imaging.ImageAttributes Attributes = new System.Drawing.Imaging.ImageAttributes();
Attributes.SetColorMatrix(NewColorMatrix);
NewGraphics.DrawImage(TempBitmap, new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), 0, 0, TempBitmap.Width, TempBitmap.Height, System.Drawing.GraphicsUnit.Pixel, Attributes);
NewGraphics.Dispose();
NewBitmap.Save(myblack+filename, System.Drawing.Imaging.ImageFormat.Jpeg);


اگه اخرین خط کد رو به کد زیر تغغیر بدید برنامه اوکی میشه
string img1 = "2.jpg";
NewBitmap.Save(Server .MapPath (img1 ), System.Drawing.Imaging.ImageFormat.Jpeg);