[英]How do I make that the first item in ListBox will be selected automatically?
[英]How do i make that when i select different item in the listBox it will show me the cropped image for that item?
列表框選擇索引事件:
private void listBoxSnap_SelectedIndexChanged(object sender, EventArgs e)
{
WindowSnap snap = this.listBoxSnap.SelectedItem as WindowSnap;
selectedIndex = this.listBoxSnap.SelectedIndex.ToString();
this.pictureBoxSnap.Image = snap.Image;
for (int i = 0; i < rectangles.Length; i++)
{
if (rectangles[i] != RectClone)
{
ClearGraphics = false;
}
else
{
ClearGraphics = true;
}
}
if (rectangles[listBoxSnap.SelectedIndex].Width == 0 && rectangles[listBoxSnap.SelectedIndex].Height == 0)
{
Reset.Enabled = false;
ConfirmRectangle.Enabled = false;
cm.MenuItems[0].Enabled = false;
cm.MenuItems[1].Enabled = false;
cm.MenuItems[2].Enabled = false;
}
if (rectangles[listBoxSnap.SelectedIndex].Width > 5 && rectangles[listBoxSnap.SelectedIndex].Height > 5)
{
Reset.Enabled = true;
if (IsRectangleConfirmed == true)
{
ConfirmRectangle.Enabled = false;
ClearGraphics = true;
cropRect = true;
}
else
{
ConfirmRectangle.Enabled = true;
}
cm.MenuItems[0].Enabled = true;
cm.MenuItems[1].Enabled = true;
cm.MenuItems[2].Enabled = true;
}
}
和pictureBox的paint事件:
private void pictureBoxSnap_Paint(object sender, PaintEventArgs e)
{
if (pictureBoxSnap.Image != null)
{
{
if (ClearGraphics == false)
{
if (rectangles[listBoxSnap.SelectedIndex] != Rectangle.Empty)
{
e.Graphics.DrawRectangle(Pens.Firebrick, rectangles[listBoxSnap.SelectedIndex]);
}
}
if (cropRect == true)
{
if (recttest.Width > 5 && recttest.Height > 5)
{
pnt = PointToScreen(pictureBoxSnap.Location);
e.Graphics.Clear(Color.White);
e.Graphics.CopyFromScreen(pnt.X + rect.X, pnt.Y + rect.Y, rect.X, rect.Y, new Size(rect.Width, rect.Height));
}
}
}
}
}
問題出在這部分:
pnt = PointToScreen(pictureBoxSnap.Location);
e.Graphics.Clear(Color.White);
e.Graphics.CopyFromScreen(pnt.X + rect.X, pnt.Y + rect.Y, rect.X, rect.Y, new Size(rect.Width, rect.Height));
此部分繪制裁剪后的矩形,但是當我在列表框中選擇另一個項目,然后再次使用裁剪后的矩形返回到該項目時,它將再次繪制它,我希望它只是記住它並根據所選項目再次顯示它。
這部分:
if (IsRectangleConfirmed == true)
{
ConfirmRectangle.Enabled = false;
ClearGraphics = true;
cropRect = true;
}
IF:if(IsRectangleConfirmed == true)表示我現在單擊了一個按鈕,並為此項目im創建了一個裁剪矩形。 問題是,當我每次回到該項目時,裁剪后的矩形將再次繪制,並且我希望它只顯示出來,就像它會記住此選定項目(索引)的裁剪后的矩形一樣。
我想做的是幾件事:
當我在pictureBox上繪制矩形並在listBox中的項目之間移動時,它將記住那些已繪制矩形的項目。 這部分正在工作。
當我單擊按鈕ConfirmRectangle_Click時,它將根據我繪制的矩形制作一個裁剪的矩形,我希望當我在listBox中的項目之間移動時,它會記住那些帶有裁剪圖像的項目。
此行獲取最小化的Windows屏幕截圖:
this.listBoxSnap.Items.AddRange(WindowSnap.GetAllWindows(true,true).ToArray());
我將它們添加到listBox,並且在項目之間移動時,我在pictureBox中看到每個屏幕截圖。 我現在想對已裁剪圖像的項目僅截取裁剪圖像部分的屏幕截圖。 如果沒有裁剪的圖像,請定期從整個圖像中截取屏幕截圖。
這是我在zip文件中的項目,名稱為CaptureZip
這是我在winrar名稱Capture中的項目:
根據您以前的帖子,您嘗試通過鼠標拖動來裁剪圖像。 現在,當您選擇另一個項目后再次選擇該項目時,您想找回該裁剪的圖像。 在這種情況下,裁剪圖像后,您無需將裁剪后的圖像設置回SelectedItem
因此當您再次選擇該項目時,它將顯示裁剪后的圖像而不是原始圖像。
Image img = CropImage();
((WindowSnap)listBox.SelectedItem).Image = img;
或者,您可以在WindowSnap
類中創建另一個屬性。
即。 public image CroppedImage {get; set;}
因此,當您選擇項目時,應檢查是否裁切了項目。 如果是這樣,則可以顯示裁剪的圖像而不是原始圖像。
WindowSnap snap = this.listBoxSnap.SelectedItem as WindowSnap;
selectedIndex = this.listBoxSnap.SelectedIndex.ToString();
///Here you can draw image at your desire position as you have done using e.DrawImage
///in pictureBoxSnap_Paint event instead of assigning pictureBoxSnap.Image property
this.pictureBoxSnap.Image = snap.CroppedImage
;
將pictureBoxSnap SizeMode
設置為normal
。
private bool[] isCropped;
private Image img;
private Image imgClone;
public Form1()
{
InitializeComponent();
img = new Bitmap(pictureBoxSnap.Width, pictureBoxSnap.Height);
imgClone = new Bitmap(pictureBoxSnap.Width, pictureBoxSnap.Height);
Graphics g;
using (g = Graphics.FromImage(img))
{
g.Clear(Color.White);
}
pictureBoxSnap.Image = img;
...
...
rectangles = new Rectangle[listBoxSnap.Items.Count];
isCropped = new bool[listBoxSnap.Items.Count];
...
}
private void listBoxSnap_SelectedIndexChanged(object sender, EventArgs e)
{
drawpicbox(this.listBoxSnap.SelectedIndex);
}
private void drawpicbox(int index)
{
Graphics g, g1;
Size sz;
WindowSnap snap = this.listBoxSnap.SelectedItem as WindowSnap;
Bitmap testBmp;
using (g = Graphics.FromImage(img))
{
g.Clear(Color.White);
sz = calculateSizeAndPosition(snap.Image.Size);
if (isCropped[index] == true)
{
ConfirmRectangle.Enabled = false;
using (testBmp = new Bitmap (img.Width , img.Height )){
using (g1 = Graphics.FromImage(testBmp))
{
g1.Clear(Color.White);
g1.DrawImage(snap.Image, (int)((pictureBoxSnap.Width - sz.Width) / 2.0), (int)((pictureBoxSnap.Height - sz.Height) / 2.0), sz.Width, sz.Height);
}
g.DrawImage(testBmp, rectangles[index], rectangles[index], GraphicsUnit.Pixel);
g.DrawRectangle(Pens.Firebrick, rectangles[index]);
}
}
else if (rectangles[index].Width != 0)
{
ConfirmRectangle.Enabled = true;
g.DrawImage(snap.Image, (int)((pictureBoxSnap.Width - sz.Width) / 2.0), (int)((pictureBoxSnap.Height - sz.Height) / 2.0), sz.Width, sz.Height);
g.DrawRectangle(Pens.Firebrick, rectangles[index]);
}
else
{
ConfirmRectangle.Enabled = false;
g.DrawImage(snap.Image, (int)((pictureBoxSnap.Width - sz.Width) / 2.0), (int)((pictureBoxSnap.Height - sz.Height) / 2.0), sz.Width, sz.Height);
}
}
pictureBoxSnap.Invalidate();
}
private Size calculateSizeAndPosition(Size snapSize)
{
int wdth, hgt;
Single flt;
wdth = snapSize.Width;
hgt = snapSize.Height;
flt = (Single)wdth / (Single)hgt;
if (wdth <= pictureBoxSnap.Width && hgt <= pictureBoxSnap.Height)
{
//return new Size(wdth, hgt);
}
else{
if(wdth >= hgt){
while (true)
{
wdth--;
hgt = (int)(wdth / flt);
if (wdth <= pictureBoxSnap.Width && hgt <= pictureBoxSnap.Height)
{
break;
}
}
}
else{
while (true)
{
hgt--;
wdth = (int)((Single)hgt * flt);
if (wdth <= pictureBoxSnap.Width && hgt <= pictureBoxSnap.Height)
{
break;
}
}
}
}
return new Size(wdth, hgt);
}
private void pictureBoxSnap_MouseMove(object sender, MouseEventArgs e)
{
if (isCropped[listBoxSnap.SelectedIndex] == false && e.Button == MouseButtons.Left && e.Location != RectStartPoint)
{
DrawRectangle(e.Location);
}
}
private void DrawRectangle(Point pnt)
{
Graphics g = Graphics.FromImage(img);
//g.DrawRectangle(Pens.Firebrick, 50, 50, 300, 200);
g.DrawImage(imgClone, 0, 0);
if (pnt.X == RectStartPoint.X || pnt.Y == RectStartPoint.Y)
{
g.DrawLine(Pens.Firebrick, RectStartPoint.X, RectStartPoint.Y, pnt.X, pnt.Y);
}
else if (pnt.X > RectStartPoint.X && pnt.Y > RectStartPoint.Y) //Right-Down
{
rect.X = RectStartPoint.X; rect.Y = RectStartPoint.Y; rect.Width = pnt.X - RectStartPoint.X; rect.Height = pnt.Y - RectStartPoint.Y;
g.DrawRectangle(Pens.Firebrick, rect);
}
else if (pnt.X > RectStartPoint.X && pnt.Y < RectStartPoint.Y) //Right-Up
{
rect.X = RectStartPoint.X; rect.Y = pnt.Y; rect.Width = pnt.X - RectStartPoint.X; rect.Height = RectStartPoint.Y - pnt.Y;
g.DrawRectangle(Pens.Firebrick, rect);
}
else if (pnt.X < RectStartPoint.X && pnt.Y > RectStartPoint.Y) //Left-Down
{
rect.X = pnt.X; rect.Y = RectStartPoint.Y; rect.Width = RectStartPoint.X - pnt.X; rect.Height = pnt.Y - RectStartPoint.Y;
g.DrawRectangle(Pens.Firebrick, rect);
}
else //Left-Up
{
rect.X = pnt.X; rect.Y = pnt.Y; rect.Width = RectStartPoint.X - pnt.X; rect.Height = RectStartPoint.Y - pnt.Y;
g.DrawRectangle(Pens.Firebrick, rect);
}
g.Dispose();
pictureBoxSnap.Invalidate();
}
private void pictureBoxSnap_MouseDown(object sender, MouseEventArgs e)
{
Graphics g;
Size sz;
if (isCropped[listBoxSnap.SelectedIndex] == false && e.Button == MouseButtons.Left)
{
WindowSnap snap = this.listBoxSnap.SelectedItem as WindowSnap;
RectStartPoint = e.Location;
sz = calculateSizeAndPosition(snap.Image.Size);
using (g = Graphics.FromImage(imgClone))
{
g.Clear(Color.White);
g.DrawImage(snap.Image, (int)((pictureBoxSnap.Width - sz.Width) / 2.0), (int)((pictureBoxSnap.Height - sz.Height) / 2.0), sz.Width, sz.Height);
}
}
}
private void pictureBoxSnap_MouseUp(object sender, MouseEventArgs e)
{
if (isCropped[listBoxSnap.SelectedIndex] == false && e.Button == MouseButtons.Left && e.Location.X != RectStartPoint.X && e.Location.Y != RectStartPoint.Y)
{
ConfirmRectangle.Enabled = true;
rectangles[listBoxSnap.SelectedIndex] = rect;
//drawpicbox(this.listBoxSnap.SelectedIndex);
}
}
private void ConfirmRectangle_Click(object sender, EventArgs e)
{
isCropped[this.listBoxSnap.SelectedIndex] = true;
drawpicbox(this.listBoxSnap.SelectedIndex);
}
private void Reset_Click(object sender, EventArgs e)
{
isCropped[this.listBoxSnap.SelectedIndex] = false;
rectangles[this.listBoxSnap.SelectedIndex] = new Rectangle(0, 0, 0, 0);
drawpicbox(this.listBoxSnap.SelectedIndex);
}
private void pictureBoxSnap_Paint(object sender, PaintEventArgs e)
{
//Nothing
}
private void RefreshWindowsList()
{
Graphics g;
g = GraphicsfromImage(img);
g.Clear(Color.White);
ClearGraphics = true;
this.listBoxSnap.Items.Clear();
buttonSnap.Enabled = false;
this.listBoxSnap.Items.AddRange(WindowSnap.GetAllWindows(true, true).ToArray());
buttonSnap.Enabled = true;
for (int i = listBoxSnap.Items.Count - 1; i >= 0; i--)
{
string tt = listBoxSnap.Items[i].ToString();
if (tt.Contains(" ,"))
{
listBoxSnap.Items.RemoveAt(i);
}
}
rectangles = new Rectangle[listBoxSnap.Items.Count];
isCropped = new bool[listBoxSnap.Items.Count];
ConfirmRectangle.Enabled = false;
textBoxIndex.Text = listBoxSnap.Items.Count.ToString();
if (this.listBoxSnap.Items.Count > 0)
this.listBoxSnap.SetSelected(0, true);
listBoxSnap.Select();
pictureBoxSnap.Invalidate();
}
編輯
一些小的更正:
private void RefreshWindowsList()
{
//Graphics g; <- You dont need this
//g = GraphicsfromImage(img); <- You dont need this
//g.Clear(Color.White); <- You dont need this
//ClearGraphics = true; <- You dont need this
this.listBoxSnap.Items.Clear();
buttonSnap.Enabled = false;
this.listBoxSnap.Items.AddRange(WindowSnap.GetAllWindows(true, true).ToArray());
buttonSnap.Enabled = true;
for (int i = listBoxSnap.Items.Count - 1; i >= 0; i--)
{
string tt = listBoxSnap.Items[i].ToString();
if (tt.Contains(" ,"))
{
listBoxSnap.Items.RemoveAt(i);
}
}
rectangles = new Rectangle[listBoxSnap.Items.Count];
isCropped = new bool[listBoxSnap.Items.Count];
//ConfirmRectangle.Enabled = false; <- You dont need this
textBoxIndex.Text = listBoxSnap.Items.Count.ToString();
if (this.listBoxSnap.Items.Count > 0)
this.listBoxSnap.SetSelected(0, true);
listBoxSnap.Select();
//pictureBoxSnap.Invalidate(); <- You dont need this
}
將private void DrawRectangle(Point pnt)
替換為:
private void DrawRectangle(Point pnt)
{
Graphics g = Graphics.FromImage(img);
g.DrawImage(imgClone, 0, 0);
if (pnt.X == RectStartPoint.X || pnt.Y == RectStartPoint.Y)
{
g.DrawLine(Pens.Firebrick, RectStartPoint.X, RectStartPoint.Y, pnt.X, pnt.Y);
}
else
{
g.DrawRectangle(Pens.Firebrick, Math.Min(RectStartPoint.X, pnt.X), Math.Min(RectStartPoint.Y, pnt.Y),
Math.Abs(RectStartPoint.X - pnt.X), Math.Abs(RectStartPoint.Y - pnt.Y));
}
g.Dispose();
pictureBoxSnap.Invalidate();
}
將private void pictureBoxSnap_MouseUp(object sender, MouseEventArgs e)
替換為:
private void pictureBoxSnap_MouseUp(object sender, MouseEventArgs e)
{
if (isCropped[listBoxSnap.SelectedIndex] == false && e.Button == MouseButtons.Left)
{
if (Math.Abs(e.Location.X - RectStartPoint.X) < 10 || Math.Abs(e.Location.Y - RectStartPoint.Y) < 10)
{
rectangles[listBoxSnap.SelectedIndex] = new Rectangle(0, 0, 0, 0);
drawpicbox(listBoxSnap.SelectedIndex);
}
else
{
ConfirmRectangle.Enabled = true;
rectangles[listBoxSnap.SelectedIndex] = new Rectangle(Math.Min(RectStartPoint.X, e.X), Math.Min(RectStartPoint.Y, e.Y),
Math.Abs(RectStartPoint.X - e.X), Math.Abs(RectStartPoint.Y - e.Y));
}
}
}
一些解釋:
函數calculateSizeAndPosition()
(可能應該是calculateSize()
),計算snap
圖像的新大小以適合圖片框。 它以類似於圖片框縮放模式的方式進行計算。
img
是pictureBoxSnap.Invalidate();
之后picturebox始終繪制的內容pictureBoxSnap.Invalidate();
。 因此,如果要對img
框進行任何更改,請使用img
,然后使其無效。
瓦爾特
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.