Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ImageSample.cs 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /***************************************************************************************
  2. DocX – DocX is the community edition of Xceed Words for .NET
  3. Copyright (C) 2009-2017 Xceed Software Inc.
  4. This program is provided to you under the terms of the Microsoft Public
  5. License (Ms-PL) as published at http://wpftoolkit.codeplex.com/license
  6. For more features and fast professional support,
  7. pick up Xceed Words for .NET at https://xceed.com/xceed-words-for-net/
  8. *************************************************************************************/
  9. using System;
  10. using System.Drawing;
  11. using System.Drawing.Imaging;
  12. using System.IO;
  13. using System.Linq;
  14. namespace Xceed.Words.NET.Examples
  15. {
  16. public class ImageSample
  17. {
  18. #region Private Members
  19. private const string ImageSampleResourcesDirectory = Program.SampleDirectory + @"Image\Resources\";
  20. private const string ImageSampleOutputDirectory = Program.SampleDirectory + @"Image\Output\";
  21. #endregion
  22. #region Constructors
  23. static ImageSample()
  24. {
  25. if( !Directory.Exists( ImageSample.ImageSampleOutputDirectory ) )
  26. {
  27. Directory.CreateDirectory( ImageSample.ImageSampleOutputDirectory );
  28. }
  29. }
  30. #endregion
  31. #region Public Methods
  32. /// <summary>
  33. /// Add a picture loaded from disk or stream to a document.
  34. /// </summary>
  35. public static void AddPicture()
  36. {
  37. Console.WriteLine( "\tAddPicture()" );
  38. // Create a document.
  39. using( DocX document = DocX.Create( ImageSample.ImageSampleOutputDirectory + @"AddPicture.docx" ) )
  40. {
  41. // Add a title
  42. document.InsertParagraph( "Adding Pictures" ).FontSize( 15d ).SpacingAfter( 50d ).Alignment = Alignment.center;
  43. // Add a simple image from disk.
  44. var image = document.AddImage( ImageSample.ImageSampleResourcesDirectory + @"balloon.jpg" );
  45. var picture = image.CreatePicture( 150, 150 );
  46. var p = document.InsertParagraph( "Here is a simple picture added from disk:" );
  47. p.AppendPicture( picture );
  48. p.SpacingAfter( 30 );
  49. // Add a rotated image from disk.
  50. var rotatedPicture = image.CreatePicture( 150, 150 );
  51. rotatedPicture.Rotation = 25;
  52. var p2 = document.InsertParagraph( "Here is the same picture added from disk, but rotated:" );
  53. p2.AppendPicture( rotatedPicture );
  54. p2.SpacingAfter( 30 );
  55. // Add a simple image from a stream
  56. var streamImage = document.AddImage( new FileStream( ImageSample.ImageSampleResourcesDirectory + @"balloon.jpg", FileMode.Open, FileAccess.Read ) );
  57. var pictureStream = streamImage.CreatePicture( 150, 150 );
  58. var p3 = document.InsertParagraph( "Here is the same picture added from a stream:" );
  59. p3.AppendPicture( pictureStream );
  60. document.Save();
  61. Console.WriteLine( "\tCreated: AddPicture.docx\n" );
  62. }
  63. }
  64. /// <summary>
  65. /// Copy a picture from a paragraph.
  66. /// </summary>
  67. public static void CopyPicture()
  68. {
  69. Console.WriteLine( "\tCopyPicture()" );
  70. // Create a document.
  71. using( DocX document = DocX.Create( ImageSample.ImageSampleOutputDirectory + @"CopyPicture.docx" ) )
  72. {
  73. // Add a title
  74. document.InsertParagraph( "Copying Pictures" ).FontSize( 15d ).SpacingAfter( 50d ).Alignment = Alignment.center;
  75. // Add a paragraph containing an image.
  76. var image = document.AddImage( ImageSample.ImageSampleResourcesDirectory + @"balloon.jpg" );
  77. var picture = image.CreatePicture( 100, 100 );
  78. var p = document.InsertParagraph( "This is the first paragraph. " );
  79. p.AppendPicture( picture );
  80. p.AppendLine("It contains an image added from disk.");
  81. p.SpacingAfter( 50 );
  82. // Add a second paragraph containing no image.
  83. var p2 = document.InsertParagraph( "This is the second paragraph. " );
  84. p2.AppendLine( "It contains a copy of the image located in the first paragraph." ).AppendLine();
  85. // Extract the first Picture from the first Paragraph.
  86. var firstPicture = p.Pictures.FirstOrDefault();
  87. if( firstPicture != null )
  88. {
  89. // copy it at the end of the second paragraph.
  90. p2.AppendPicture( firstPicture );
  91. }
  92. document.Save();
  93. Console.WriteLine( "\tCreated: CopyPicture.docx\n" );
  94. }
  95. }
  96. /// <summary>
  97. /// Modify an image from a document by writing text into it.
  98. /// </summary>
  99. public static void ModifyImage()
  100. {
  101. Console.WriteLine( "\tModifyImage()" );
  102. // Open the document Input.docx.
  103. using( DocX document = DocX.Load( ImageSample.ImageSampleResourcesDirectory + @"Input.docx" ) )
  104. {
  105. // Add a title
  106. document.InsertParagraph( 0, "Modifying Image by adding text/circle into the following image", false ).FontSize( 15d ).SpacingAfter( 50d ).Alignment = Alignment.center;
  107. // Get the first image in the document.
  108. var image = document.Images.FirstOrDefault();
  109. if( image != null )
  110. {
  111. // Create a bitmap from the image.
  112. var bitmap = new Bitmap( image.GetStream( FileMode.Open, FileAccess.ReadWrite ) );
  113. // Get the graphic from the bitmap to be able to draw in it.
  114. var graphic = Graphics.FromImage( bitmap );
  115. if( graphic != null )
  116. {
  117. // Draw a string with a specific font, font size and color at (0,10) from top left of the image.
  118. graphic.DrawString( "@copyright", new System.Drawing.Font( "Arial Bold", 12 ), Brushes.Red, new PointF( 0f, 10f ) );
  119. // Draw a blue circle of 10x10 at (30, 5) from the top left of the image.
  120. graphic.FillEllipse( Brushes.Blue, 30, 5, 10, 10 );
  121. // Save this Bitmap back into the document using a Create\Write stream.
  122. bitmap.Save( image.GetStream( FileMode.Create, FileAccess.Write ), ImageFormat.Png );
  123. }
  124. }
  125. document.SaveAs( ImageSample.ImageSampleOutputDirectory + @"ModifyImage.docx" );
  126. Console.WriteLine( "\tCreated: ModifyImage.docx\n" );
  127. }
  128. }
  129. #endregion
  130. }
  131. }