【问题标题】:Save an image and text as one将图像和文本保存为一个
【发布时间】:2017-02-23 15:25:17
【问题描述】:

我目前正在制作一个 MVC 项目,用户将收到一个显示在页面上的证书。证书是一个图像,然后 Text 用于与将打印用户名和其他信息的图像重叠,使用 css 将文本格式化为图像的正确部分。

由于图像和文本在技术上仍然是分开的,因此保存图像不会将文本保存在顶部,因此您保存的只是证书模板,没有文本。

有没有办法将图像和文本保存为一个,就好像文本被压在图像上并且是同一个对象一样?如果是这样,我将不胜感激被指出正确的方向以知道如何做到这一点。任何将图像和文本保存为一体的想法或代码都会非常有帮助。

谢谢。

【问题讨论】:

    标签: c# html css model-view-controller


    【解决方案1】:

    将图形保存为 SVG 并使用文本对象添加文本。

    您还可以在 SVG 中使用 CSS 来设置文本样式。

    <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
        <text x="20" y="40">Example SVG text 1</text>
    </svg>
    

    【讨论】:

      【解决方案2】:

      希望对你有帮助

      private void makeCertificate(string name, string id, string otherDetails) //You can pass any other details as well 
      {
          System.Drawing.Image PrePrintedCertificate;
          name = name.ToUpper();
      
          string PrePrintedCertificateName = "Certificate.jpg"; //Assuming Certificate JPG File is in the bin folder
          using (FileStream stream = new FileStream(PrePrintedCertificateName, FileMode.Open, FileAccess.Read))
          {
              PrePrintedCertificate = System.Drawing.Image.FromStream(stream);
          }
      
          RectangleF rectf4Name = new RectangleF(655, 460, 535, 90); //rectf for Name
          RectangleF rectf4ID = new RectangleF(655, 560, 400, 40); 
          System.Drawing.Rectangle rect;
      
          Bitmap picEdit = new Bitmap(PrePrintedCertificate, new System.Drawing.Size(1241, 1756));
      
          using (Graphics g = Graphics.FromImage(picEdit))
          {
              //g.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.Red, 2), 662, 530, 90, 40);
              //I have used upper line to see where is my RectangleF creating on the image
              g.SmoothingMode = SmoothingMode.AntiAlias;
              g.InterpolationMode = InterpolationMode.HighQualityBicubic;
              g.PixelOffsetMode = PixelOffsetMode.HighQuality;
              StringFormat sf = new StringFormat();
              sf.Alignment = StringAlignment.Center;
              StringFormat sf1 = new StringFormat();
              sf1.Alignment = StringAlignment.Near;
      
              //g.DrawImage(codeImage, rect); //If you wanted to draw another image on the certificate image
              g.DrawString(name, new System.Drawing.Font("Thaoma", 26, System.Drawing.FontStyle.Bold), System.Drawing.Brushes.Black, rectf4Name, sf);
              g.DrawString(Track.Text, new System.Drawing.Font("Thaoma", 14, System.Drawing.FontStyle.Bold), System.Drawing.Brushes.Black, rectf4ID, sf1);
          }
          try
          {
              if (File.Exists(id + ".jpg"))
                  File.Delete(id + ".jpg");
      
              picEdit.Save(id + ".jpg", ImageFormat.Jpeg);
              picEdit.Dispose();
          }
          catch (Exception e)
          {
              MessageBox.Show(e.Message);
          }
      }
      

      这里要注意的是,此图像是A4 尺寸的纵向纸张图像。您可能需要将Bitmap picEdit = new Bitmap(PrePrintedCertificate, new System.Drawing.Size(1241, 1756)); 更改为Bitmap picEdit = new Bitmap(PrePrintedCertificate, new System.Drawing.Size(1756,1241));

      另一件事是名称和其他详细信息已随机打印在图像上,但您可以看到您希望打印详细信息的确切位置。

      您可以使用g.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.Red, 2), 662, 530, 90, 40);查看它的打印位置,您将在此处传递的参数将与 RectangleF 参数相同。

      【讨论】:

      • 嗨@James,如果这个或任何答案解决了您的问题,请点击复选标记考虑accepting it。这向更广泛的社区表明您已经找到了解决方案,并为回答者和您自己提供了一些声誉。没有义务这样做。
      • 您好,很抱歉没有尽快回复,我不得不完全擦除我的电脑。感谢您的回答,我确实设法找到了解决问题的方法(我将图像和文本转换为 PDF),您的代码确实帮助我找到了这个解决方案。真的很感激。
      猜你喜欢
      • 2021-10-12
      • 1970-01-01
      • 1970-01-01
      • 2011-03-08
      • 1970-01-01
      • 2021-11-02
      • 1970-01-01
      • 2019-04-27
      • 1970-01-01
      相关资源
      最近更新 更多