훈스닷넷에 PictureBox위에 1cm 간격으로 눈금을 그리고 싶어하는 질문이 있어서
데모 프로젝트를 작성해보았습니다.
코드는 아래와 같습니다.
public partial class Form1 : Form { /// <summary> /// 1pixel에 해당하는 센치 /// </summary> const float OnePixelCentimetre = 37.79f; /// <summary> /// 수직라인 길이 /// </summary> const int VerticalLineHeight = 50; /// <summary> /// 그리드 라인 색상 /// </summary> readonly Pen gridLineColor;
public Form1() { InitializeComponent(); this.pictureBox1.Image = Image.FromFile("./Chrysanthemum.jpg");
this.pictureBox1.Paint += PictureBox1_PaintEventHandler; this.SizeChanged += Form1_SizeChangedEventHandler; this.gridLineColor = Pens.Yellow; }
/// <summary> /// 폼의 사이즈 변경 이벤트 처리기 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_SizeChangedEventHandler(object sender, EventArgs e) { this.pictureBox1.Invalidate(); }
/// <summary> /// PictureBox의 Paint 이벤트 처리기 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void PictureBox1_PaintEventHandler(object sender, PaintEventArgs e) { PictureBox pb = (PictureBox)sender; Graphics g = e.Graphics;
//Picturebox의 중앙 위치 float centerYPosition = pb.Height / 2.0f;
//수평 라인 그리기 g.DrawLine(gridLineColor, 0f, centerYPosition, pb.Width, centerYPosition);
//현재 Picturbox에서 1cm 개수 double cmStep = pb.Width / OnePixelCentimetre;
//세로 라인의 절반 int verticalLineHeightHalf = VerticalLineHeight / 2;
//수직 라인 그리기 for (float i = 0; i < pb.Width; i += OnePixelCentimetre) { PointF beginPoint = new PointF(i, centerYPosition - verticalLineHeightHalf); PointF endPoint = new PointF(i, centerYPosition + verticalLineHeightHalf); g.DrawLine(gridLineColor, beginPoint, endPoint); } } }
|
프로젝트(vs2015 작성) : HowToDrawOnPicturebox.zip
C# PictureBox 투명도 조절 (이미지 투명도 조절) (0) | 2015.10.12 |
---|---|
C# Visual Studio와 같은 컨트롤 배치 컨트롤 (2) | 2015.07.10 |
C# Winform 멀티 UI 쓰레드 (0) | 2015.05.06 |
C# DataGridView 콤보박스 클릭시 리스트 보여지게 하기 (0) | 2014.10.31 |
C# Excel 2007 추가기능 - 엑셀 Cell의 스크린 위치 구하기 (0) | 2014.08.22 |
개발 중인 프로젝트에서 상용 컴퍼넌트를 이용하다보니 제작한 라이브러리 포함하여 dll이 30개 정도 되었다.
고객님께서는 dll이 너무 많아 복잡하다며 실행 파일 한개만을 원하셨다.
제프리 리쳐 형님과 같이 동적으로 어셈블리를 로드할까 했는데....좀 더 검색해보았다.
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => { String resourceName = “AssemblyLoadingAndReflection.” + new AssemblyName(args.Name).Name + “.dll”; using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) { Byte[] assemblyData = new Byte[stream.Length]; stream.Read(assemblyData, 0, assemblyData.Length); return Assembly.Load(assemblyData); } }; |
결국 Costumra.Fody 를 찾게 되었다.
자동으로 exe 안에 라이브러리를 넣어준다. 압축까지 지원해준다.
Nuget을 통하여 설치하면 "FodyWeavers.xml" 파일이 생성된다.
해당 xml 파일을 이용해 정교한 설정이 가능하다. (압축여부, Debug심볼첨부여부 등등...)
https://github.com/Fody/Costura
압축하였을 때는 .zip 확장자로 exe 파일에 삽입되어있다.
[압축하지 않은 상태]
[압축한 상태]
C# 리플렉션을 이용한 Delegate 넘기기 (0) | 2016.05.11 |
---|---|
C# Undo Redo 기능 구현하기 (3) | 2015.10.20 |
C# 원문자 구하기 (0) | 2015.10.05 |
SerialPort Read Blocking 시키기 (0) | 2015.08.05 |
문자열 비교 테스트 (대소문자 무시) (0) | 2015.07.02 |