Guest User

Untitled

a guest
Feb 16th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.70 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.IO;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Drawing;
  8. using System.Drawing.Imaging;
  9. using System.Drawing.Drawing2D;
  10. using System.Drawing.Text;
  11.  
  12. namespace ConsoleApplication1
  13. {
  14. class BeiAnImageUtil
  15. {
  16. private String folder = "E:\\";
  17.  
  18. public byte[] createImage(String sourceImagePath, String beiAnNumber, String beiAnStampImagePath)
  19. {
  20. Bitmap bitMapImage = new System.Drawing.Bitmap(sourceImagePath);
  21. return this.createImage(bitMapImage, beiAnNumber, beiAnStampImagePath);
  22. }
  23.  
  24. public byte[] createImage(Bitmap bitMapImage, String beiAnNumber, String beiAnStampImagePath)
  25. {
  26. Graphics graphicImage = Graphics.FromImage(bitMapImage);
  27.  
  28. //Smooth graphics is nice.
  29. graphicImage.SmoothingMode = SmoothingMode.AntiAlias;
  30.  
  31. //Write your text.
  32. //graphicImage.DrawString("That's my boy!",
  33. // new Font("Arial", 12, FontStyle.Bold),
  34. // SystemBrushes.WindowText, new Point(0, 0));
  35.  
  36. Font font = new Font("Tahoma", 16);
  37. int x_axis = 910;
  38. int y_axis = 110;
  39. int distance = 25;
  40. graphicImage.DrawString(beiAnNumber, font, Brushes.Black, new PointF(x_axis, y_axis));
  41. graphicImage.DrawString(DateTime.Now.ToString("yyyy-MM-dd"), font, Brushes.Black, new PointF(x_axis + 90, y_axis + distance));
  42. String validDate = DateTime.Now.AddYears(2).ToString("yyyy-MM-dd");
  43. graphicImage.DrawString("有效期至:" + validDate, font, Brushes.Black, new PointF(x_axis, y_axis + distance * 2));
  44.  
  45. String tempFile = folder + "test.jpeg";
  46. bitMapImage.Save(tempFile, ImageFormat.Jpeg);
  47. MemoryStream ms = new MemoryStream();
  48. //bitMapImage.Save(ms, ImageFormat.Jpeg);
  49. //graphicImage.Save();
  50.  
  51. //I am drawing a oval around my text.
  52. //graphicImage.DrawArc(new Pen(Color.Red, 3), 90, 235, 150, 50, 0, 360);
  53.  
  54. //Set the content type
  55. //Response.ContentType = "image/jpeg";
  56. //Save the new image to the response output stream.
  57. //bitMapImage.Save(Response.OutputStream, ImageFormat.Jpeg);
  58.  
  59. //Clean house.
  60. //graphicImage.Dispose();
  61. //bitMapImage.Dispose();
  62. byte[] bytes = MergeJpeg(tempFile, beiAnStampImagePath, folder + "output.jpeg");
  63.  
  64. //Delete temp file
  65. if (System.IO.File.Exists(tempFile))
  66. {
  67. // Use a try block to catch IOExceptions, to
  68. // handle the case of the file already being
  69. // opened by another process.
  70. try
  71. {
  72. System.IO.File.Delete(tempFile);
  73. }
  74. catch (System.IO.IOException e)
  75. {
  76. Console.WriteLine(e.Message);
  77. return bytes;
  78. }
  79. }
  80. return bytes;
  81. }
  82. /// <summary>
  83. /// Merges two Jpeg images vertically
  84. /// </summary>
  85. /// <param name="inputJpeg1">filename with complete path of the first jpeg file.</param>
  86. /// <param name="inputJpeg2">filname with complete path of the second jpeg file.</param>
  87. /// <param name="outputJpeg">filename with complete path where you want to save the output jpeg file.</param>
  88. private byte[] MergeJpeg(string inputJpeg1, string inputJpeg2, string outputJpeg)
  89. {
  90.  
  91. Image image1 = Image.FromFile(inputJpeg1);
  92. Image image2 = Image.FromFile(inputJpeg2);
  93.  
  94. int width = Math.Max(image1.Width, image2.Width);
  95. int height = image1.Height + image2.Height;
  96.  
  97. Bitmap outputImage = new Bitmap(width, height);
  98. Graphics graphics = Graphics.FromImage(outputImage);
  99.  
  100. graphics.Clear(Color.Black);
  101. graphics.DrawImage(image1, new Point(0, 0));
  102.  
  103. Point stampPoint = new Point(100, 20);
  104. graphics.DrawImage(image2, stampPoint);
  105. //graphics.DrawImage(image2, new Point(0, image1.Height - image2.Height));
  106.  
  107. graphics.Dispose();
  108. image1.Dispose();
  109. image2.Dispose();
  110.  
  111. MemoryStream stream = new MemoryStream();
  112. //outputImage.Save(outputJpeg, System.Drawing.Imaging.ImageFormat.Jpeg);
  113. outputImage.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
  114. outputImage.Dispose();
  115. return stream.ToArray();
  116. }
  117.  
  118. public Bitmap ByteArraytoBitmap(Byte[] byteArray)
  119. {
  120. MemoryStream stream = new MemoryStream(byteArray);
  121.  
  122. return new System.Drawing.Bitmap(stream);
  123. }
  124.  
  125. public byte[] imageToByteArray(System.Drawing.Image imageIn)
  126. {
  127. MemoryStream ms = new MemoryStream();
  128. imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
  129. return ms.ToArray();
  130. }
  131.  
  132.  
  133. static void Main(string[] args)
  134. {
  135. BeiAnImageUtil util = new BeiAnImageUtil();
  136. String folder = "E:\\";
  137. Bitmap bitMapImage = new System.Drawing.Bitmap(folder + "test.jpg");
  138. byte[] imageBytes = util.imageToByteArray(bitMapImage);
  139. //util.createImage(folder + "test.jpg", "SN-XQ-S-201311250025", folder + "test2.jpg");
  140. Bitmap b = util.ByteArraytoBitmap(imageBytes);
  141.  
  142. byte[] output = util.createImage(b, "SN-XQ-S-201311250025", folder + "test2.jpg");
  143. Bitmap bm = util.ByteArraytoBitmap(output);
  144. bm.Save(folder+"output.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
  145. }
  146.  
  147.  
  148.  
  149. }
  150. }
Add Comment
Please, Sign In to add comment