PDA

View Full Version : سوال: چگونه روی تصویر Picture box یه Rectangular Selection ایجاد کنم؟



Mofid.m
یک شنبه 08 دی 1392, 15:07 عصر
من تو فرمم یه Picture Box دارم که می خوام قسمتی از اون تصویر رو انتخاب کنم.
یعنی تصویر توی Picture box یه Resolution داره که میخوام با ی Rectangular Selection مشخص کنم که از کدوم مختصات رزولوشن انتخاب شده و عرض و ارتفاع رزولوشن چیه؟
میخوام وقتی فرمم Show شد دور تصویر Picture Box یه کادر بیاد که گوشه هاش گیره داره و با انتخاب اون گیره ها اندازه رو تغییر بدم

salehsam
یک شنبه 08 دی 1392, 15:16 عصر
سلام دوست من
من با یه روش دیگه این کارو انجام دادم خودمون مستقیم انتخاب میکنیم!
کدهاشو براتون میگذارم
وارد کردن تصویر :

try
{
OpenFileDialog op = new OpenFileDialog();
op.ShowDialog();
b = new Bitmap(op.FileName);
c = op.FileName;
pic_img.Image = b;
}
catch
{
MessageBox.Show("لطفا تصویری را وارد نمائید");

}
شروع انتخاب با فشار دادن دکمه موس :


private void pic_img_MouseDown(object sender, MouseEventArgs e)
{
// Starting point of the selection:
if (e.Button == MouseButtons.Left)
{
_selecting = true;
_selection = new Rectangle(new Point(e.X, e.Y), new Size());

}
}
انتخاب با درگ کردن موس:


private void pic_img_MouseMove(object sender, MouseEventArgs e)
{
// Update the actual size of the selection:
if (_selecting)
{
_selection.Width = e.X - _selection.X;
_selection.Height = e.Y - _selection.Y;


// Redraw the picturebox:
pic_img.Refresh();
}
}


اتمام انتخاب با رها کردن دکمه موس:


private void pic_img_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && _selecting)
{
if (_selection.Height > 0 && _selection.Width > 0)
{
pic_img.Image = cropImage(pic_img.Image, _selection);

_selecting = false;
}
}

}
برای کروپ کردن حتما به این رویداد نیاز دارد!


private static Image cropImage(Image img, Rectangle cropArea)
{

Bitmap bmpImage = new Bitmap(img);
Bitmap bmpCrop = bmpImage.Clone(cropArea,
bmpImage.PixelFormat);
return (Image)(bmpCrop);
}
برای نمایش محل انتخاب شده یا همون Rectangular Selection :


private void pic_img_Paint(object sender, PaintEventArgs e)
{
if (_selecting)
{
// Draw a rectangle displaying the current selection
Pen pen = Pens.AliceBlue;
e.Graphics.DrawRectangle(pen, _selection);
}
}

این هم کل سورس برای چرخاندن و کروپ و رفرش تصویر :


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace meybodrcs
{
public partial class frm_edit_image : Form
{

public string frm;

public frm_edit_image()
{
InitializeComponent();
}
MemoryStream ms = new MemoryStream();
MemoryStream msimg = new MemoryStream();
private bool _selecting;
private Rectangle _selection;
Bitmap b;
string c;

private void button1_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog op = new OpenFileDialog();
op.ShowDialog();
b = new Bitmap(op.FileName);
c = op.FileName;
pic_img.Image = b;
}
catch
{
MessageBox.Show("لطفا تصویری را وارد نمائید");

}
}

private void pic_img_MouseDown(object sender, MouseEventArgs e)
{
// Starting point of the selection:
if (e.Button == MouseButtons.Left)
{
_selecting = true;
_selection = new Rectangle(new Point(e.X, e.Y), new Size());

}
}

private void pic_img_MouseMove(object sender, MouseEventArgs e)
{
// Update the actual size of the selection:
if (_selecting)
{
_selection.Width = e.X - _selection.X;
_selection.Height = e.Y - _selection.Y;


// Redraw the picturebox:
pic_img.Refresh();
}
}

private void pic_img_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && _selecting)
{
if (_selection.Height > 0 && _selection.Width > 0)
{
pic_img.Image = cropImage(pic_img.Image, _selection);

_selecting = false;
}
}

}

private static Image cropImage(Image img, Rectangle cropArea)
{

Bitmap bmpImage = new Bitmap(img);
Bitmap bmpCrop = bmpImage.Clone(cropArea,
bmpImage.PixelFormat);
return (Image)(bmpCrop);
}

private void pic_img_Paint(object sender, PaintEventArgs e)
{
if (_selecting)
{
// Draw a rectangle displaying the current selection
Pen pen = Pens.AliceBlue;
e.Graphics.DrawRectangle(pen, _selection);
}
}


private void btn_RotateNoneFlipY_Click(object sender, EventArgs e)
{
Image im = pic_img.Image;
im.RotateFlip(RotateFlipType.RotateNoneFlipY);
pic_img.Image = im;
}

private void btn_RotateNoneFlipX_Click(object sender, EventArgs e)
{
Image im = pic_img.Image;
im.RotateFlip(RotateFlipType.RotateNoneFlipX);
pic_img.Image = im;
}

private void btn_Rotate90FlipNone_Click(object sender, EventArgs e)
{
Image im = pic_img.Image;
im.RotateFlip(RotateFlipType.Rotate90FlipNone);
pic_img.Image = im;
}

private void btn_Rotate270FlipNone_Click(object sender, EventArgs e)
{
Image im = pic_img.Image;
im.RotateFlip(RotateFlipType.Rotate270FlipNone);
pic_img.Image = im;
}

private void button3_Click(object sender, EventArgs e)
{
frm_editperson.img = null;
frm_editperson.img = pic_img.Image;
pic_img.Image = null;
pic_img.Invalidate();
pic_img.Update();
this.Close();
}

private void frm_edit_image_Load(object sender, EventArgs e)
{
pic_img.Image = null;
pic_img.Invalidate();
frm_editperson.img = null;
}

private void btn_refresh_Click(object sender, EventArgs e)
{
pic_img.Image = new Bitmap(c);

}

private void btn_open_MouseHover(object sender, EventArgs e)
{
ToolTip t = new ToolTip();
t.Show("وارد کردن تصویر از فایل", btn_open);
}


}
}


سوالی بود در خدمتم :لبخندساده:

salehsam
یک شنبه 08 دی 1392, 15:23 عصر
البته بگم برای درست کار کردنش باید خاصیت size mode پیکچرباکست رو روی auto size(اگه میخوای خوب باشه بذار تو یه پنل) یا نرمال قرار بدی!

Mofid.m
یک شنبه 08 دی 1392, 15:55 عصر
ممنون دوست عزیز