Crop area selection control(like photoshop's) in c# windows form - c#-4.0

how to develop a crop selection control like photoshop's in c# 4.0 in widows form application.
I have a windows form application in c# 4.0 that can crop images. At first you have to draw a rectangle using mouse to select the cropped region.
private Point _pt;
private Point _pt2;
private void picBoxImageProcessing_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
int ix = (int)(e.X / _zoom);
int iy = (int)(e.Y / _zoom);
//reset _pt2
_pt2 = new Point(0, 0);
_pt = new Point(ix, iy);
// pictureBox1.Invalidate();
picBoxImageProcessing.Invalidate();
}
}
private void picBoxImageProcessing_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && _selecting)
{
_selecting = false;
}
}
private void picBoxImageProcessing_Paint(object sender, PaintEventArgs e)
{
if (_selecting &&_pt.X >= 0 && _pt.Y >= 0 && _pt2.X >= 0 && _pt2.Y >= 0)
{
e.Graphics.DrawRectangle(pen, _pt.X * _zoom, _pt.Y * _zoom,
(_pt2.X - _pt.X) * _zoom, (_pt2.Y - _pt.Y) * _zoom);
}
}
private void picBoxImageProcessing_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
_selecting = true;
int ix = (int)(e.X / _zoom);
int iy = (int)(e.Y / _zoom);
_pt2 = new Point(ix, iy);
// pictureBox1.Invalidate();
picBoxImageProcessing.Invalidate();
}
}
there is no problem to draw the rectangle by mouse dragging. But if i want to change the height or width of the rectangle then I have to draw a new rectangle, that i don't want. I want to change the height and width of the rectangle by modifying the drawn rectangle instead of drawing a new rectangle.
I don’t want to know how to crop. I need to draw a resizable rectangle on the image as we can do in photoshop.
So I need a crop selection control like photoshop's crop control.

Related

Draw Graphics on bitmap

I try to use mouse up and mouse down to draw rectangles on bitmaps. But the problem is that the rectangle always delay one event. For example, I try to draw a rectangle (0,0,50,50) in the first time but there is no rectangle drawing on bitmap. I continues drawing a rect (50,50,100,100) but a rect (0,0,50,50) created (not be the rect (50,50,100,100). If I keep to draw next rects, it always delay like that. Please help me!
This is my code:
Rectangle rect = new Rectangle(0, 0, 0, 0);
int Xmouse;
int Ymouse;
public Form1()
{
InitializeComponent();
pictureBox1.Paint += new PaintEventHandler(pictureBox1_Paint);
this.DoubleBuffered = true;
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
Xmouse = e.X;
Ymouse = e.Y;
drawOK = true;
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (drawOK)
{
drawOK = false;
rect = new Rectangle(Xmouse * 3676 / 800, Ymouse * 3676 / 800, (e.X - Xmouse) * 3676 / 800, (e.Y - Ymouse) * 3676 / 800);
pictureBox1.Invalidate();
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
using (var g = Graphics.FromImage(pictureBox1.Image))
{
using (Pen myPen = new Pen(Color.Black, 6))
{
g.DrawRectangle(myPen, rect);
}
}
}
Try moving your draw method outside of paint into a different sub and call that sub manually from mouse up:
Rectangle rect = new Rectangle(0, 0, 0, 0);
int Xmouse;
int Ymouse;
bool drawOK;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
Xmouse = e.X;
Ymouse = e.Y;
Console.WriteLine("MouseDown({0},{1})", e.X, e.Y);
drawOK = true;
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (drawOK)
{
drawOK = false;
Console.WriteLine("MouseUp({0},{1})", e.X, e.Y);
rect = new Rectangle(Xmouse , Ymouse , (e.X - Xmouse) , (e.Y - Ymouse) );
DrawRect();
pictureBox1.Invalidate();
}
}
private void DrawRect()
{
Console.WriteLine("drawing {0}", rect);
using (var g = Graphics.FromImage(pictureBox1.Image))
{
using (Pen myPen = new Pen(Color.Black, 6))
{
g.DrawRectangle(myPen, rect);
}
}
}
private void ClearPic()
{
using (var g = Graphics.FromImage(pictureBox1.Image))
{
using (Brush myPen = Brushes.White)
{
g.Clear(Color.White);
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
Image bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
pictureBox1.Image = bmp;
ClearPic();
pictureBox1.Paint += new PaintEventHandler(pictureBox1_Paint);
pictureBox1.MouseDown += pictureBox1_MouseDown;
pictureBox1.MouseUp += pictureBox1_MouseUp;
}

Clearing the canvas with J2ME

I'm trying to create a program that shows two images at random locations and changes them every second. My problem however is when the image is redrawn I can still see the image in the previous position.
I'm using WTK 2.52 if that's relevant.
public void run()
{
while(true)
{
dVert = rand.nextInt(136);
dHorz = rand.nextInt(120);
rVert = 136 + rand.nextInt(136);
rHorz = 120 + rand.nextInt(120);
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
System.out.println("Error");
}
repaint();
}
}
public void paint(Graphics g)
{
int width = this.getWidth() / 2;
int height = this.getHeight() / 2;
g.drawImage(imgR, rVert, rHorz, g.TOP | g.LEFT);
g.drawImage(imgD,dVert,dHorz,g.TOP | g.LEFT);
//g.drawString(disp, width, 50, Graphics.TOP | Graphics.HCENTER);
//g.drawString(centerMsg,width, height, Graphics.TOP | Graphics.HCENTER);
System.out.println(width);
System.out.println(height);
}
Complete code
You clear the canvas like this:
g.setColor(0x000000); // Whatever color you wish to clear with
g.setClip(0,0,getWidth(),getHeight());
g.fillRect(0,0,getWidth(),getHeight());
So just insert those lines before drawing the images.

MFC: How to draw in a static text

I try to draw in a static text control named IDC_RESULT.When I click the OK button, the static text will display different picture according to the shape I selected.So I declare three bool variable: isLine ,is Rect and isEllipse, everytime when I choose Lien radio box, I make the bool variabel isLine be true and the others be false, the same as Rectangle and Ellipse.
This is my code:
void CDrawDlg::OnPaint()
{
CWnd* pWnd = (CWnd*)GetDlgItem(IDC_RESULT);
CPaintDC dcPaint(pWnd);
CPen pen(PS_SOLID,2,RGB(0,0,0));
dcPaint.SelectObject(&pen);
CRect rect;
pWnd->GetClientRect(&rect);
if(isLine)
{
dcPaint.MoveTo(10,150);
dcPaint.LineTo (350,150);
}
if(isRect)
{
dcPaint.Rectangle(50, 100, 300, 200);
}
if(isEllipse)
{
doSomething;
}
ReleaseDC(&dcPaint);
if (IsIconic())
{
CPaintDC dc(this);
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
And the function below associate with the OK button:
void CDrawDlg::OnBnClickedOk()
{
Invalidate(false);
}
The question is: How to erase the line in the static text control when I select Rectangle and click the OK button to draw a rectangle,but I can't erase the line.
The parameter to Invalidate (bErase) controls whether or not the background within the update region should be erased. Since you passed false it isn't. Pass TRUE instead.
As an aside, you should use TRUE/FALSE instead of true/false as arguments to MFC methods. Even though the types are compatible they are still different.

How to prevent screen rotation in j2me?

I'm programming in j2me.
How can I prevent screen rotation in j2me for all phones that support screen rotation?
thanks.
If you use Canvas to draw your screen (not LCDUI, not LWUIT, not any other framework) you may implement sizeChanged method to be notified when the rotation happens.
In such case you may draw your screen to an Image and use Sprite to rotate it. For example, to support only Landscape mode I used below code at constructor:
int width = Math.max(super.getWidth(), super.getHeight());
int height = Math.min(super.getWidth(), super.getHeight());
// screen and sprite are attributes
screen = Image.createImage(width, height);
sprite = new Sprite(screen);
if (super.getWidth() < super.getHeight()) { // portrait screen
sprite.setTransform(Sprite.TRANS_ROT90);
sprite.setPosition(0, 0);
}
And following methods:
public void sizeChanged (int w, int h) {
// lastWidth and lastHeight are attributes
lastWidth = w;
lastHeight = h;
if (sprite == null) return;
if (super.getWidth() < super.getHeight()) { // portrait screen
sprite.setTransform(Sprite.TRANS_ROT90);
} else {
sprite.setTransform(Sprite.TRANS_NONE);
}
sprite.setPosition(0, 0);
}
protected void paint(Graphics g1) {
if (super.getWidth() != lastWidth
|| super.getHeight() != lastHeight) {
sizeChanged(super.getWidth(), super.getHeight());
}
Graphics g = screen.getGraphics();
// ... do your drawing on g
this.sprite.setImage(screen, screen.getWidth(), screen.getHeight());
sprite.paint(g1);
}
As seen at http://smallandadaptive.blogspot.com.br/2009/08/fullscreen-landscape.html and http://smallandadaptive.blogspot.com.br/2010/03/adapting-to-sizechanged.html and http://smallandadaptive.blogspot.com.br/2011/04/sizechanged-not-called.html
Add to manifest
For Nokia devices:
Nokia-MIDlet-App-Orientation : Landscape
For Samsung devices:
MIDlet-ScreenMode : Landscape

Emulated docking the form is sized down, upon removing from dock the form only stays full size when it has focus with a mouse down

Calling module:
private void Indicator_Move(object sender, EventArgs e)
{
Sizer size = new Sizer();
int x = this.Location.X;
int y = this.Location.Y;
int width = this.Width;
int height = this.Height;
var screenWidth = Screen.PrimaryScreen.WorkingArea.Width - 130;
if (x >= screenWidth)
{
size.checkmove(x, y, width, height, this);
}
else if (width == 158 )
{
width = 235;
height = 223;
this.Size = new System.Drawing.Size(width, height);
}
}
Module to Dock and reduce size from Sizer class:
public void checkmove(int movex, int movey,int width, int height, Form mover)
{
var moverheight = mover.Height;
var screenWidth = Screen.PrimaryScreen.WorkingArea.Width - 130;
var screenHeight = Screen.PrimaryScreen.WorkingArea.Height;
var finalx = screenWidth;
if (movex > screenWidth)
{
mover.Size = new System.Drawing.Size(154, 45);
mover.Location = new Point(finalx, screenHeight - 600);
}
}
I actually found a different way to accomplish what I wanted by the double_click event on the form and just moving it out on to the desktop it full size, but I am still curious why it resizes back and forth to full size and reduced size while moving and appearing to flicker and ending up reduced and only showing full size when the mouse button is clicked on form and held down. I'm thinking it has to do with the resizeend call anytime the form is moved. And thanks Joran for the edit.

Resources