将图像保存到数据库
你好,我制作了一个程序,可以在按下按钮时将我的绘图保存到图像中,但我需要将其推送到 MySql 数据库中,有人知道怎么做吗?
这就是我的图像保存程序的工作原理(主要用于绘制签名)。
我将使用 Visual Studio Microsoft SQL 数据库。
public Werkbon()
{
InitializeComponent();
}
Pen somePen = new Pen(Color.Black, 3.0f); // make a black pen with 3,0 width
List<Point> currentLine = new List<Point>();
List<List<Point>> curves = new List<List<Point>>();
public void panel1_Paint(object sender, PaintEventArgs e)
{
using (Pen somePen = new Pen(Color.Black, 3.0f))
{
if (currentLine.Count > 1) e.Graphics.DrawCurve(somePen, currentLine.ToArray());
foreach (List<Point> lp in curves)
if (lp.Count > 1) e.Graphics.DrawCurve(somePen, lp.ToArray());
}
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
currentLine.Add(e.Location);
panel1.Invalidate();
}
}//END method
private void button3_Click(object sender, EventArgs e)
{
curves.Clear(); currentLine.Clear(); panel1.Invalidate(); // clear the field
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
if (currentLine.Count > 1) curves.Add(currentLine.ToList());
currentLine.Clear();
panel1.Invalidate();
}
//database vv
private void Save_Click_1(object sender, EventArgs e)
{
SaveFileDialog save = new SaveFileDialog();
save.CheckFileExists = false;
save.CheckPathExists = true;
save.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg;
*.jpe; *.jfif; *.png";
save.InitialDirectory = @"C:\images\";
DialogResult result = save.ShowDialog();
if (result == DialogResult.OK)
{
Bitmap bmp = new Bitmap(panel1.Width, panel1.Height);
panel1.DrawToBitmap(bmp, new Rectangle(0, 0, panel1.Width, panel1.Height));
bmp.Save(save.FileName);
}
}
}
Hi i made a program that saves my drawing to a image on button press, but i need to push it into a MySql database, anyone know how?
this is how my image saving program works(its mostly used to draw signatures).
i will be using the visual studio Microsoft SQL Database.
public Werkbon()
{
InitializeComponent();
}
Pen somePen = new Pen(Color.Black, 3.0f); // make a black pen with 3,0 width
List<Point> currentLine = new List<Point>();
List<List<Point>> curves = new List<List<Point>>();
public void panel1_Paint(object sender, PaintEventArgs e)
{
using (Pen somePen = new Pen(Color.Black, 3.0f))
{
if (currentLine.Count > 1) e.Graphics.DrawCurve(somePen, currentLine.ToArray());
foreach (List<Point> lp in curves)
if (lp.Count > 1) e.Graphics.DrawCurve(somePen, lp.ToArray());
}
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
currentLine.Add(e.Location);
panel1.Invalidate();
}
}//END method
private void button3_Click(object sender, EventArgs e)
{
curves.Clear(); currentLine.Clear(); panel1.Invalidate(); // clear the field
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
if (currentLine.Count > 1) curves.Add(currentLine.ToList());
currentLine.Clear();
panel1.Invalidate();
}
//database vv
private void Save_Click_1(object sender, EventArgs e)
{
SaveFileDialog save = new SaveFileDialog();
save.CheckFileExists = false;
save.CheckPathExists = true;
save.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg;
*.jpe; *.jfif; *.png";
save.InitialDirectory = @"C:\images\";
DialogResult result = save.ShowDialog();
if (result == DialogResult.OK)
{
Bitmap bmp = new Bitmap(panel1.Width, panel1.Height);
panel1.DrawToBitmap(bmp, new Rectangle(0, 0, panel1.Width, panel1.Height));
bmp.Save(save.FileName);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在大多数情况下,推荐将图像存储在文件系统中,并将该文件的路径保存在数据库中。
因此,您应该能够保留现有代码以将文件保存到某个配置的存储文件夹,并使用数据库中的列来存储此路径。您还可以考虑存储相对路径,并引用具有根文件夹绝对路径的单独表。但是,当您拥有大量图像并且需要多个磁盘进行存储时,这非常有用。
添加新列以及如何使用所述列的确切说明可能超出了此问题的范围。
In most cases it is recommended to store images in the file system, and save the path to this file in the database.
So you should be able to keep your existing code to save the file to some configured storage folder, and use a column in the database to store this path. You might also consider storing a relative path, and reference a separate table that has the absolute path to the root folder. But this is mostly helpful when you have huge number of images, and need multiple disks for storage.
The exact instructions for adding a new column, and how to use said column might be out of scope for this question.