在winforms中保存图像 [英] image saving in winforms

查看:88
本文介绍了在winforms中保存图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Windows应用程序中将图像保存到数据库中

解决方案

尝试此方法.当您要存储图像的字段是bytea类型时,它应该可以工作.首先,它为图像创建byte [].然后使用二进制类型的IDataParameter将其保存到数据库.

 公共 静态  void  PerisitImage(字符串路径,IDbConnection连接)
    {
        使用( var  command = connection.CreateCommand())
        {
            图片img = Image.FromFile(路径);
            MemoryStream tmpStream =  MemoryStream();
            img.Save(tmpStream,ImageFormat.Png); // 更改为其他格式
            tmpStream.Seek( 0 ,SeekOrigin.Begin);
            字节 [] imgBytes =  字节 [ MAX_IMG_SIZE];
            tmpStream.Read(imgBytes, 0 ,MAX_IMG_SIZE);

            command.CommandText = " ;
            IDataParameter par = command.CreateParameter();
            par.ParameterName = " ;
            par.DbType = DbType.Binary;
            参数值= imgBytes;
            command.Parameters.Add(par);
            command.ExecuteNonQuery();
        }
    } 


请参考以下主题:

示例程序演示如何在SQL Server中保存或存储图像:
在SQL Server中存储或保存图像 [ ^ ]

此示例代码说明了如何使用C#在SQL Server数据库中存储或保存图像.它使用ADO.Net System.Data.SqlClient namespace
使用C#在SQL Server数据库中存储或保存图像 [^ ]

使用C#Windows应用程序将图像插入数据库. [ ^ ]




转到此
在SQL Server中存储或保存图像 [ ^ ]

how to save image into database in windows applications

解决方案

Try this method. It should work when field when you want to store image is of type bytea. First it creates byte[] for image. Then it saves it DB using IDataParameter of type binary.

public static void PerisitImage(string path, IDbConnection connection)
    {
        using (var command = connection.CreateCommand ())
        {
            Image img = Image.FromFile (path);
            MemoryStream tmpStream = new MemoryStream();
            img.Save (tmpStream, ImageFormat.Png); // change to other format
            tmpStream.Seek (0, SeekOrigin.Begin);
            byte[] imgBytes = new byte[MAX_IMG_SIZE];
            tmpStream.Read (imgBytes, 0, MAX_IMG_SIZE);

            command.CommandText = "INSERT INTO images(payload) VALUES (:payload)";
            IDataParameter par = command.CreateParameter();
            par.ParameterName = "payload";
            par.DbType = DbType.Binary;
            par.Value = imgBytes;
            command.Parameters.Add(par);
            command.ExecuteNonQuery ();
        }
    }


Please refer following threads:

A sample program to demonstrate how to save or store images in SQL server:
Store or Save images in SQL Server[^]

This sample code explains you how you can store or save images in SQL Server database using C#. It uses ADO.Net System.Data.SqlClient namespace
Store or save images in SQL Server database using C#[^]

Inserting image into database using C# windows application.[^]


Hi,

Go to this
Store or Save images in SQL Server[^]


这篇关于在winforms中保存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆