提问人:Przemo 提问时间:11/17/2023 最后编辑:maciekPrzemo 更新时间:11/17/2023 访问量:83
在 Blazor 中打印映像
Printing an image in Blazor
问:
我有一个存储用户数据的 Blazor 服务器应用程序。每个用户都有自己的登录详细信息和登录代码。现在我有一个问题,我想从浏览器输出此代码。我有freeSpire.Barcode库。现在从客户端来看:进入应用程序页面时,我想打印代码,但没有任何反应。这意味着它将应用程序保存在服务器上的文件,但不会打开它。您是否有一种行之有效的方法将文件从客户端打印到服务器?
public void PrintCodeUser(int id)
{
string imie = "";
string nazwisko = "";
string kod = "";
using (SqlConnection con1 = new SqlConnection(connectionToSql))
{
string sqll = " SELECT Imie,Nazwisko FROM CompositeApps.dbo.Kamino WHERE id =" + id;
try
{
var cmdd = new SqlCommand(sqll, con1);
con1.Open();
SqlDataReader rdrr = cmdd.ExecuteReader();
while (rdrr.Read())
{
if ((rdrr.IsDBNull(0))) { imie = "0"; } else { imie = rdrr[0].ToString(); }
if ((rdrr.IsDBNull(1))) { nazwisko = ""; } else { nazwisko = rdrr[1].ToString(); }
if ((rdrr.IsDBNull(2))) { kod = ""; } else { kod = rdrr[2].ToString(); }
}
con1.Close();
}
catch { /*MessageBox.Show("Error :)");*/ }
}
////////////////// poczatek daty ////////////////
string firstText = imie + " " + nazwisko;
string firstTextt = kod ;
PointF firstLocation = new PointF(10f, 10f);
PointF firstLocationn = new PointF(10f, 10f);
PointF firstLocationnn = new PointF(10f, 10f);
string imageFilePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\tempkod11.png";
Bitmap bitmap = (Bitmap)System.Drawing.Image.FromFile(imageFilePath);//load the image file
using (Graphics graphics = Graphics.FromImage(bitmap))
{
//using (Font arialFont = new Font("Arial", 11))
//{
// graphics.DrawString(firstText, arialFont, Brushes.Black, firstLocation);
//}
//using (Font arialFont = new Font("Arial", 8))
//{
// graphics.DrawString(firstTextt, arialFont, Brushes.Black, firstLocation);
//}
using (Font arialFont = new Font("Arial", 14))
{
string napis = firstText;// + firstTextt;
graphics.DrawString(napis, arialFont, Brushes.Black, firstLocationnn);
}
}
bitmap.Save(@"C:\CompositeApps\tempkod22.png");//save the image file
bitmap.Dispose();
//DialogResult dialogResult = MessageBox.Show("Otworzyc plik do druku ? ? ?", "Zapytajnik", MessageBoxButtons.YesNo);
//if (dialogResult == DialogResult.Yes)
{
Process process = new Process();
process.StartInfo.UseShellExecute = true;
process.StartInfo.FileName = "msedge.exe";
process.StartInfo.Arguments = @"C:\CompositeApps\tempkod22.png"; //@"C:\temp\csct.json";
process.Start();
// System.Diagnostics.Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\tempkod22.png");
// System.Diagnostics.Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\tempkod22.png");
}
//else if (dialogResult == DialogResult.No)
{
// MessageBox.Show("Anulowano podglad");
}
}
答:
1赞
Stephen Weatherhead
11/17/2023
#1
据我了解,您希望显示生成的图像,或将其下载给用户。如果将图像保存到流而不是文件,则可以:
- 将图像嵌入页面中,如Microsoft文档 https://learn.microsoft.com/en-us/aspnet/core/blazor/images?view=aspnetcore-6.0#stream-image-data 中所示
- 触发图像下载,如下所示:https://learn.microsoft.com/en-us/aspnet/core/blazor/file-downloads?view=aspnetcore-6.0#download-from-a-stream
可以使用 MemoryStream 并使用位图中的 Save 重载将其输出到流:
using (MemoryStream myStream = new MemoryStream)
{
bitmap.Save(myStream, ImageFormat.Png);
}
评论