📄 form1.h
字号:
#pragma once
namespace Example
{
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Form1 摘要
///
/// 警告: 如果您更改该类的名称,则需要更改
/// 与该类所依赖的所有 .resx 文件关联的托管资源编译器工具的
/// “资源文件名”属性。 否则,
/// 设计器将不能与此窗体关联的
/// 本地化资源正确交互。
/// </summary>
public __gc class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
}
protected:
void Dispose(Boolean disposing)
{
if (disposing && components)
{
components->Dispose();
}
__super::Dispose(disposing);
}
private: System::Windows::Forms::PictureBox * pictureBox1;
private: System::Windows::Forms::Label * label1;
private: System::Windows::Forms::TextBox * textBox1;
private: System::Windows::Forms::Button * button1;
private: System::Windows::Forms::Button * button2;
private: System::Windows::Forms::Button * button3;
private: System::Windows::Forms::OpenFileDialog * openFileDialog1;
private: System::Windows::Forms::SaveFileDialog * saveFileDialog1;
private:
/// <summary>
/// 必需的设计器变量。
/// </summary>
System::ComponentModel::Container * components;
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
void InitializeComponent(void)
{
System::Resources::ResourceManager * resources = new System::Resources::ResourceManager(__typeof(Example::Form1));
this->pictureBox1 = new System::Windows::Forms::PictureBox();
this->label1 = new System::Windows::Forms::Label();
this->textBox1 = new System::Windows::Forms::TextBox();
this->button1 = new System::Windows::Forms::Button();
this->button2 = new System::Windows::Forms::Button();
this->button3 = new System::Windows::Forms::Button();
this->openFileDialog1 = new System::Windows::Forms::OpenFileDialog();
this->saveFileDialog1 = new System::Windows::Forms::SaveFileDialog();
this->SuspendLayout();
//
// pictureBox1
//
this->pictureBox1->BorderStyle = System::Windows::Forms::BorderStyle::Fixed3D;
this->pictureBox1->Image = (__try_cast<System::Drawing::Image * >(resources->GetObject(S"pictureBox1.Image")));
this->pictureBox1->Location = System::Drawing::Point(0, 0);
this->pictureBox1->Name = S"pictureBox1";
this->pictureBox1->Size = System::Drawing::Size(344, 176);
this->pictureBox1->SizeMode = System::Windows::Forms::PictureBoxSizeMode::StretchImage;
this->pictureBox1->TabIndex = 0;
this->pictureBox1->TabStop = false;
//
// label1
//
this->label1->Location = System::Drawing::Point(0, 192);
this->label1->Name = S"label1";
this->label1->Size = System::Drawing::Size(72, 16);
this->label1->TabIndex = 1;
this->label1->Text = S"标记文本:";
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(64, 184);
this->textBox1->Name = S"textBox1";
this->textBox1->Size = System::Drawing::Size(88, 21);
this->textBox1->TabIndex = 2;
this->textBox1->Text = S"图片天下";
//
// button1
//
this->button1->Location = System::Drawing::Point(152, 184);
this->button1->Name = S"button1";
this->button1->Size = System::Drawing::Size(64, 23);
this->button1->TabIndex = 3;
this->button1->Text = S"浏览";
this->button1->Click += new System::EventHandler(this, button1_Click);
//
// button2
//
this->button2->Location = System::Drawing::Point(216, 184);
this->button2->Name = S"button2";
this->button2->Size = System::Drawing::Size(64, 23);
this->button2->TabIndex = 4;
this->button2->Text = S"添加";
this->button2->Click += new System::EventHandler(this, button2_Click);
//
// button3
//
this->button3->Location = System::Drawing::Point(280, 184);
this->button3->Name = S"button3";
this->button3->Size = System::Drawing::Size(64, 23);
this->button3->TabIndex = 5;
this->button3->Text = S"保存";
this->button3->Click += new System::EventHandler(this, button3_Click);
//
// openFileDialog1
//
this->openFileDialog1->Filter = S"Image files (JPeg, Gif, Bmp, etc.)|*.jpg;*.jpeg;*.gif;*.bmp;*.tif; *.tiff; *.png|"
S" JPeg files (*.jpg;*.jpeg)|*.jpg;*.jpeg |GIF files (*.gif)|*.gif |BMP files (*.b"
S"mp)|*.bmp|Tiff files (*.tif;*.tiff)|*.tif;*.tiff|Png files (*.png)| *.png |All f"
S"iles (*.*)|*.*";
//
// Form1
//
this->AutoScaleBaseSize = System::Drawing::Size(6, 14);
this->ClientSize = System::Drawing::Size(344, 214);
this->Controls->Add(this->button3);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->label1);
this->Controls->Add(this->pictureBox1);
this->MaximizeBox = false;
this->Name = S"Form1";
this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
this->Text = S"演示在图像上添加标记";
this->ResumeLayout(false);
}
private: System::Void button1_Click(System::Object * sender, System::EventArgs * e)
{//浏览
if(this->openFileDialog1->ShowDialog()==DialogResult::OK)
{
this->pictureBox1->Image=System::Drawing::Bitmap::FromFile(this->openFileDialog1->FileName);
}
}
private: System::Void button2_Click(System::Object * sender, System::EventArgs * e)
{//添加
Image* MyImage=this->pictureBox1->Image;
int MyFont=300;
bool bMyFound;
int MyWidth=MyImage->Width;
int MyHeight=MyImage->Height;
int size=MyWidth*MyHeight;
Bitmap* MyPattern= new Bitmap(MyWidth,MyHeight);
Bitmap* MyBitmap = new Bitmap(MyImage);
Graphics* g = Graphics::FromImage(MyPattern);
Graphics* MyGraphics =Graphics::FromImage(MyImage);
bMyFound=false;
g->Clear(Color::White);
while(bMyFound==false)
{
System::Drawing::Font* NewFont = new System::Drawing::Font("Georgia", MyFont, System::Drawing::FontStyle::Bold);
SizeF MyString= System::Drawing::SizeF(MyWidth,MyHeight);
MyString=g->MeasureString(this->textBox1->Text,NewFont);
if(MyString.Width<MyPattern->Width)
{
if (MyString.Height<MyPattern->Height)
{
bMyFound=true;
g->DrawString(this->textBox1->Text, NewFont, new SolidBrush(Color::Black),1,1);
}
}
else
MyFont=MyFont-1;
}
for(int x=1;x<MyPattern->Width;x++)
{
for(int y=1;y<MyPattern->Height;y++)
{
int a,re,gr,bl,x1,y1,z1;
if(MyPattern->GetPixel(x,y).ToArgb()==Color::Black.ToArgb())
{
a=MyBitmap->GetPixel(x,y).A;
re=MyBitmap->GetPixel(x,y).R;
gr=MyBitmap->GetPixel(x,y).G;
bl=MyBitmap->GetPixel(x,y).B;
x1=re;
y1=gr;
z1=bl;
if (bl+25<255)
bl=bl+25;
if (gr+25<255)
gr=gr+25;
if (re+25<255)
re=re+25;
if (x1-25>0)
x1=x1-25;
if (y1-25>0)
y1=y1-25;
if (z1-25>0)
z1=z1-25;
MyGraphics->DrawEllipse(new Pen(new SolidBrush(Color::Black)),x,y+1,3,3);
MyGraphics->DrawEllipse(new Pen(new SolidBrush(Color::FromArgb(a,x1,y1,z1))),x,y,1,1);
}
}
}
this->pictureBox1->Image=MyImage;
}
private: System::Void button3_Click(System::Object * sender, System::EventArgs * e)
{//保存
if(this->saveFileDialog1->ShowDialog()==DialogResult::OK)
{
Image* MyImage=this->pictureBox1->Image;
MyImage->Save(String::Concat(this->saveFileDialog1->FileName,".jpg"),System::Drawing::Imaging::ImageFormat::Jpeg);
}
}
};
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -