C\#QQ 캡 처 기능 인 스 턴 스 코드 구현
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using ImageClassLib;
namespace ImageShear
{
public partial class Demo: Form
{
public Demo()
{
InitializeComponent();
}
#region
public string strHeadImagePath; //
Bitmap Bi; //
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "*.gif|*.jpg|*.JPEG|*.JPEG|*.bmp|*.bmp"; //
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
strHeadImagePath = openFileDialog1.FileName;
//this.Show(strHeadImagePath);
Bi = new Bitmap(strHeadImagePath); //
ImageCut1 IC = new ImageCut1(40, 112, this.pictureBox1.Width, this.pictureBox1.Height); // ImageCut , :x、y、width、heigth,(40、112) pictureBox1 Lcation ,(120、144) pictureBox1
this.pictureBox1.Image = IC.KiCut1((Bitmap)(this.GetSelectImage(this.pictureBox1.Width, this.pictureBox1.Height))); //(120、144) pictureBox1
//this.pictureBox1.Image = (this.GetSelectImage(120, 144));
}
catch (Exception ex)
{
MessageBox.Show(" ");
ex.ToString();
}
}
}
#endregion
#region , pictureBox1
public void Show(string strHeadImagePath)
{
this.pictureBox1.Load(@strHeadImagePath); //
}
#endregion
#region
/// <summary>
/// pictureBox1 , Image
/// </summary>
/// <param name="Width "></param>
/// <param name="Height "></param>
/// <returns></returns>
public Image GetSelectImage(int Width, int Height)
{
//Image initImage = this.pictureBox1.Image;
Image initImage = Bi;
// , ,
if (initImage.Width <= Width && initImage.Height <= Height)
{
//initImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
return initImage;
}
else
{
// 、
int initWidth = initImage.Width;
int initHeight = initImage.Height;
//
if (initWidth != initHeight)
{
//
System.Drawing.Image pickedImage = null;
System.Drawing.Graphics pickedG = null;
//
if (initWidth > initHeight)
{
//
pickedImage = new System.Drawing.Bitmap(initHeight, initHeight);
pickedG = System.Drawing.Graphics.FromImage(pickedImage);
//
pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//
Rectangle fromR = new Rectangle((initWidth - initHeight) / 2, 0, initHeight, initHeight);
Rectangle toR = new Rectangle(0, 0, initHeight, initHeight);
//
pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
//
initWidth = initHeight;
}
//
else
{
//
pickedImage = new System.Drawing.Bitmap(initWidth, initWidth);
pickedG = System.Drawing.Graphics.FromImage(pickedImage);
//
pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//
Rectangle fromR = new Rectangle(0, (initHeight - initWidth) / 2, initWidth, initWidth);
Rectangle toR = new Rectangle(0, 0, initWidth, initWidth);
//
pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
//
initHeight = initWidth;
}
initImage = (System.Drawing.Image)pickedImage.Clone();
// //
pickedG.Dispose();
pickedImage.Dispose();
}
return initImage;
}
}
#endregion
#region button2
private void button2_Click(object sender, EventArgs e)
{
this.label1.Text = " :"+this.pictureBox2.Width.ToString();
this.label2.Text = " :"+this.pictureBox2.Height.ToString();
}
#endregion
#region 、
/// <summary>
///
/// </summary>
int x1; //
int y1; //
int width; //
int heigth; //
bool HeadImageBool = false; // pictureBox1
#endregion
#region
Point p1; //
Point p2; //
Point p3; //
#endregion
#region
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
this.Cursor = Cursors.Cross;
this.p1 = new Point(e.X, e.Y);
x1 = e.X;
y1 = e.Y;
if (this.pictureBox1.Image != null)
{
HeadImageBool = true;
}
else
{
HeadImageBool = false;
}
}
#endregion
#region
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (this.Cursor == Cursors.Cross)
{
this.p2 = new Point(e.X, e.Y);
if ((p2.X - p1.X) > 0 && (p2.Y - p1.Y) > 0) // P3
{
this.p3 = new Point(p1.X, p1.Y);
}
if ((p2.X - p1.X) < 0 && (p2.Y - p1.Y) > 0) // P3
{
this.p3 = new Point(p2.X, p1.Y);
}
if ((p2.X - p1.X) > 0 && (p2.Y - p1.Y) < 0) // P3
{
this.p3 = new Point(p1.X, p2.Y);
}
if ((p2.X - p1.X) < 0 && (p2.Y - p1.Y) < 0) // P3
{
this.p3 = new Point(p2.X, p2.Y);
}
this.pictureBox1.Invalidate(); // ,
}
}
#endregion
#region , ImageCut1
ImageCut1 IC1; //
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (HeadImageBool)
{
width = this.pictureBox1.Image.Width;
heigth = this.pictureBox1.Image.Height;
if ((e.X - x1) > 0 && (e.Y - y1) > 0) //
{
IC1 = new ImageCut1(x1, y1, Math.Abs(e.X - x1), Math.Abs(e.Y - y1)); // ImageCut1
}
if ((e.X - x1) < 0 && (e.Y - y1) > 0) //
{
IC1 = new ImageCut1(e.X, y1, Math.Abs(e.X - x1), Math.Abs(e.Y - y1)); // ImageCut1
}
if ((e.X - x1) > 0 && (e.Y - y1) < 0) //
{
IC1 = new ImageCut1(x1, e.Y, Math.Abs(e.X - x1), Math.Abs(e.Y - y1)); // ImageCut1
}
if ((e.X - x1) < 0 && (e.Y - y1) < 0) //
{
IC1 = new ImageCut1(e.X, e.Y, Math.Abs(e.X - x1), Math.Abs(e.Y - y1)); // ImageCut1
}
this.pictureBox2.Width = (IC1.KiCut1((Bitmap)(this.pictureBox1.Image))).Width;
this.pictureBox2.Height = (IC1.KiCut1((Bitmap)(this.pictureBox1.Image))).Height;
this.pictureBox2.Image = IC1.KiCut1((Bitmap)(this.pictureBox1.Image));
this.Cursor = Cursors.Default;
}
else
{
this.Cursor = Cursors.Default;
}
}
#endregion
#region
/// <summary>
///
/// </summary>
/// <param name="Width"></param>
/// <param name="Height"></param>
/// <returns></returns>
public Image GetSelectImage1(int Width, int Height)
{
Image initImage = this.pictureBox1.Image;
//Image initImage = Bi;
// , ,
if (initImage.Width <= Width && initImage.Height <= Height)
{
//initImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
return initImage;
}
else
{
// 、
int initWidth = initImage.Width;
int initHeight = initImage.Height;
//
if (initWidth != initHeight)
{
//
System.Drawing.Image pickedImage = null;
System.Drawing.Graphics pickedG = null;
//
if (initWidth > initHeight)
{
//
pickedImage = new System.Drawing.Bitmap(initHeight, initHeight);
pickedG = System.Drawing.Graphics.FromImage(pickedImage);
//
pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//
Rectangle fromR = new Rectangle((initWidth - initHeight) / 2, 0, initHeight, initHeight);
Rectangle toR = new Rectangle(0, 0, initHeight, initHeight);
//
pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
//
initWidth = initHeight;
}
//
else
{
//
pickedImage = new System.Drawing.Bitmap(initWidth, initWidth);
pickedG = System.Drawing.Graphics.FromImage(pickedImage);
//
pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//
Rectangle fromR = new Rectangle(0, (initHeight - initWidth) / 2, initWidth, initWidth);
Rectangle toR = new Rectangle(0, 0, initWidth, initWidth);
//
pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
//
initHeight = initWidth;
}
initImage = (System.Drawing.Image)pickedImage.Clone();
// //
pickedG.Dispose();
pickedImage.Dispose();
}
return initImage;
}
}
#endregion
#region pictureBox1 ,
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (HeadImageBool)
{
Pen p = new Pen(Color.Black, 1);//
p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
//Bitmap bitmap = new Bitmap(strHeadImagePath);
Bitmap bitmap = Bi;
Rectangle rect = new Rectangle(p3, new Size(System.Math.Abs(p2.X - p1.X), System.Math.Abs(p2.Y - p1.Y)));
e.Graphics.DrawRectangle(p, rect);
}
else
{
}
}
#endregion
}
}
두 번 째 부분 은 보조 방법 류 입 니 다.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace ImageClassLib
{
public class ImageCut1
{
#region
/// <summary>
/// -- GDI+
/// </summary>
/// <param name="b"> Bitmap, </param>
/// <param name="StartX"> X</param>
/// <param name="StartY"> Y</param>
/// <param name="iWidth"> </param>
/// <param name="iHeight"> </param>
/// <returns> Bitmap</returns>
public Bitmap KiCut1(Bitmap b)
{
if (b == null)
{
return null;
}
int w = b.Width;
int h = b.Height;
if (X >= w || Y >= h)
{
return null;
}
if (X + Width > w)
{
Width = w - X;
}
if (Y + Height > h)
{
Height = h - Y;
}
try
{
Bitmap bmpOut = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(bmpOut);
// Create rectangle for displaying image.
Rectangle destRect = new Rectangle(0, 0, Width, Height); // , 。 。
// Create rectangle for source image.
Rectangle srcRect = new Rectangle(X, Y, Width, Height); //srcRect image 。 destRect 。
g.DrawImage(b, destRect, srcRect, GraphicsUnit.Pixel);
//resultG.DrawImage(initImage, new System.Drawing.Rectangle(0, 0, side, side), new System.Drawing.Rectangle(0, 0, initWidth, initHeight), System.Drawing.GraphicsUnit.Pixel);
g.Dispose();
return bmpOut;
}
catch
{
return null;
}
}
#endregion
#region ImageCut1
public int X;
public int Y;
public int Width ;
public int Height;
/// <summary>
/// ImageCut1 ,ImageCut1 pictureBox1
/// </summary>
/// <param name="x pictureBox1 "></param>
/// <param name="y pictureBox1 "></param>
/// <param name="width pictureBox1 "></param>
/// <param name="heigth pictureBox1 "></param>
public ImageCut1(int x, int y, int width, int heigth)
{
X = x;
Y = y;
Width = width;
Height = heigth;
}
#endregion
}
}
실현 효 과 는 다음 과 같다.이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Rider (추가 : Visual Studio for Mac도)에서는 C#으로 작성된 Unity 표준 클래스를 직접 볼 수있는 이야기여러분은 Unity 코드를 어떤 편집기를 사용하여 작성합니까? Visual Studio (for Mac 포함) Visual Studio Code 다양한 옵션이 있다고 생각하지만 Rider를 사용하고 있습니다. 최근 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.