PDA

View Full Version : سوال: حذف قسمت های سفید دور عکس(حاشیه های اضافی عکس)



roboticsexpert
جمعه 08 اردیبهشت 1391, 17:56 عصر
سلام..

یه عکس دارم که دورش سفیده...
می خوام تا میشه دورشو حدف کنم که دیواره ی عکسم با دیواره ی فایل png یکی شه...

ممنون می شم کمکم کنید..

رافعی مهدی
جمعه 08 اردیبهشت 1391, 18:09 عصر
سلام
آیا به دنبال روشی جهت نوشتن یک برنامه برای Crop کردن تصاویر هستید؟ (اگر اینطور هست لینکهایی که قبلاً grlearn در اینجا (http://barnamenevis.org/showthread.php?282794-%D9%88%DB%8C%D8%B1%D8%A7%DB%8C%D8%B4-%D8%B9%DA%A9%D8%B3&p=1247475&viewfull=1#post1247475) معرفی کرده رو ببینید.)


private static Image cropImage(Image img, Rectangle cropArea)
{
Bitmap bmpImage = new Bitmap(img);
Bitmap bmpCrop = bmpImage.Clone(cropArea,
bmpImage.PixelFormat);
return (Image)(bmpCrop);
}

omid_kma
دوشنبه 08 اردیبهشت 1393, 22:59 عصر
سلام این لینک رو ببینید
http://www.7khatcode.com/index.php?qa=2234&qa_1=%D8%AA%D8%B4%D8%AE%DB%8C%D8%B5-%D9%88-%D8%AD%D8%B0%D9%81-%D8%AD%D8%A7%D8%B4%DB%8C%D9%87-%D8%AF%D8%B1-%D8%AA%D8%B5%D8%A7%D9%88%DB%8C%D8%B1

shahryari
سه شنبه 09 اردیبهشت 1393, 09:06 صبح
public static Bitmap CropWhiteSpace(Bitmap bmp)
{
int w = bmp.Width;
int h = bmp.Height;
int white = 0xffffff;

Func<int, bool> allWhiteRow = r =>
{
for (int i = 0; i < w; ++i)
if ((bmp.GetPixel(i, r).ToArgb() & white) != white)
return false;
return true;
};

Func<int, bool> allWhiteColumn = c =>
{
for (int i = 0; i < h; ++i)
if ((bmp.GetPixel(c, i).ToArgb() & white) != white)
return false;
return true;
};

int topmost = 0;
for (int row = 0; row < h; ++row)
{
if (!allWhiteRow(row))
break;
topmost = row;
}

int bottommost = 0;
for (int row = h - 1; row >= 0; --row)
{
if (!allWhiteRow(row))
break;
bottommost = row;
}

int leftmost = 0, rightmost = 0;
for (int col = 0; col < w; ++col)
{
if (!allWhiteColumn(col))
break;
leftmost = col;
}

for (int col = w - 1; col >= 0; --col)
{
if (!allWhiteColumn(col))
break;
rightmost = col;
}

if (rightmost == 0) rightmost = w; // As reached left
if (bottommost == 0) bottommost = h; // As reached top.

int croppedWidth = rightmost - leftmost;
int croppedHeight = bottommost - topmost;

if (croppedWidth == 0) // No border on left or right
{
leftmost = 0;
croppedWidth = w;
}

if (croppedHeight == 0) // No border on top or bottom
{
topmost = 0;
croppedHeight = h;
}

try
{
var target = new Bitmap(croppedWidth, croppedHeight);
using (Graphics g = Graphics.FromImage(target))
{
g.DrawImage(bmp,
new RectangleF(0, 0, croppedWidth, croppedHeight),
new RectangleF(leftmost, topmost, croppedWidth, croppedHeight),
GraphicsUnit.Pixel);
}
return target;
}
catch (Exception ex)
{
throw new Exception(
string.Format("Values are topmost={0} btm={1} left={2} right={3} croppedWidth={4} croppedHeight={5}", topmost, bottommost, leftmost, rightmost, croppedWidth, croppedHeight),
ex);
}
}


public void Test()
{
var inputPath = "image.png";
var outputPath = inputPath.Replace(".png", "-out.png");

var bitmap = new Bitmap(inputPath);
var cropped = CropWhiteSpace(bitmap);
cropped.Save(outputPath, ImageFormat.Png);
}