如何使用c#代码使用sql表中的链接检索图像

问题描述:

请帮帮我,

我们在SQL表中有图像路径,比如//frfriuf\jrgurvru \ rjfvrbur \ frfr \ developer.Zip

we have the images path in SQL Table, like //frfriuf\jrgurvru\rjfvrbur\frfr\developer.Zip

我在文本框中输入一个数字作为输入,该数字相关的图像路径信息存储在数据表中,所以我必须使用表数据(路径位置)检索图像,之后最初那些图像是在该路径中的Zip格式,我们必须提取
并转换为PDF格式。或者请提供分析,如何开始..请帮助我

i give a number in textbox as Input, that number related images path information stored in data table,so i have to retrieve the images by using the table data(path location),after that initially those images are in Zip format in that path, we have to extract and convert in PDF Format.or please provide the analysis, how to start..please help me

您好,

>>我们必须以PDF格式提取和转换

>> we have to extract and convert in PDF Format

试试这个:

    /// <summary>
    /// the data in Table "Zip"
    /// Id = 1, Location = "D:\test.zip";
    /// </summary>
    private void button1_Click(object sender, EventArgs e)
    {
        string zipPath;
        string connString = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Test;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
        using (SqlConnection sqlconn = new SqlConnection(connString))
        {
            sqlconn.Open();
            SqlCommand comm = new SqlCommand(


" select from Zip where(id = {Convert.ToInt32(textBox1.Text)})",sqlconn);
zipPath = comm.ExecuteScalar()。ToString();
}
string extractPath = @" D:\ test&quot ;;
ZipFile.ExtractToDirectory(zipPath,extractPath);
DirectoryInfo TheFolder = new DirectoryInfo(extractPath);
foreach(TheFolder.GetFiles()中的FileInfo NextFile)
{
//获取每个图像名称
string imagePath = extractPath + @" \" + NextFile.Name;
//将格式设置为pdf
string savePath =(extractPath + @" \" + NextFile.Name).Substring(0,imagePath.Length - 3)+" pdf&quot ;;
ConvertJPG2PDF(imagePath,savePath);
}
}

public static void ConvertJPG2PDF(string jpgfile,string pdf)
{
var document = new Document(iTextSharp.text.PageSize。 A4,25,25,25,25);
using(var stream = new FileStream(pdf,FileMode.Create,FileAccess.Write,FileShare.None))
{
PdfWriter.GetInstance(document,stream);
document.Open();
using(var imageStream = new FileStream(jpgfile,FileMode.Open,FileAccess.Read,FileShare.ReadWrite))
{
var image = iTextSharp.text.Image.GetInstance(imageStream);
if(image.Height> iTextSharp.text.PageSize.A4.Height - 25)
{
image.ScaleToFit(iTextSharp.text.PageSize.A4.Width - 25,iTextSharp。 text.PageSize.A4.Height - 25);
}
else if(image.Width> iTextSharp.text.PageSize.A4.Width - 25)
{
image.ScaleToFit(iTextSharp.text.PageSize.A4。宽度 - 25,iTextSharp.text.PageSize.A4.Height - 25);
}
image.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE;
document.NewPage();
document.Add(image);
document.NewPage();
document.Add(image);
}
document.Close();
}
}
"select Location from Zip where (id = {Convert.ToInt32(textBox1.Text)})", sqlconn); zipPath = comm.ExecuteScalar().ToString(); } string extractPath = @"D:\test"; ZipFile.ExtractToDirectory(zipPath, extractPath); DirectoryInfo TheFolder = new DirectoryInfo(extractPath); foreach (FileInfo NextFile in TheFolder.GetFiles()) { // get each image name string imagePath = extractPath + @"\" + NextFile.Name; // set the format to pdf string savePath = (extractPath + @"\" + NextFile.Name).Substring(0, imagePath.Length - 3) + "pdf"; ConvertJPG2PDF(imagePath, savePath); } } public static void ConvertJPG2PDF(string jpgfile, string pdf) { var document = new Document(iTextSharp.text.PageSize.A4, 25, 25, 25, 25); using (var stream = new FileStream(pdf, FileMode.Create, FileAccess.Write, FileShare.None)) { PdfWriter.GetInstance(document, stream); document.Open(); using (var imageStream = new FileStream(jpgfile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { var image = iTextSharp.text.Image.GetInstance(imageStream); if (image.Height > iTextSharp.text.PageSize.A4.Height - 25) { image.ScaleToFit(iTextSharp.text.PageSize.A4.Width - 25, iTextSharp.text.PageSize.A4.Height - 25); } else if (image.Width > iTextSharp.text.PageSize.A4.Width - 25) { image.ScaleToFit(iTextSharp.text.PageSize.A4.Width - 25, iTextSharp.text.PageSize.A4.Height - 25); } image.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE; document.NewPage(); document.Add(image); document.NewPage(); document.Add(image); } document.Close(); } }

在使用之前,您需要执行以下步骤:

Before using it, you need to perform the following steps:

1.  import" System.IO.Compression.FileSystem"并使用"System.IO.Compression"。

1. import "System.IO.Compression.FileSystem" and using "System.IO.Compression".

点击"添加引用..."并键入"System.IO.Compression.FileSystem"在搜索栏中安装它。

Click "Add Reference…" and type "System.IO.Compression.FileSystem" in the search bar and install it.

2.&nbsp ;添加"iTextSharp";来自"NuGet"。具体操作如下:

2. add "iTextSharp" from "NuGet". The specific operation is as follows:

右键单击Reference并选择"Manage NuGet Packages ...",然后键入"iTextSharp"。在搜索栏中安装它。并使用"iTextSharp.text"和"iTextSharp.text"。和"iTextSharp.text.pdf":

Right click the Reference and select "Manage NuGet Packages...", then type "iTextSharp" in the search bar and install it. And use "iTextSharp.text" and "iTextSharp.text.pdf":

问候,

Stanly