API (Application Programming Interface)
Namespace :
WordImageProcessing
Class name :
WordImage
Using
DocumentFormat.OpenXml.Packaging
DocumentFormat.OpenXml.Drawing
Constructors
public WordImage(string path)
public WordImage(WordprocessingDocument document)
Property
public WordprocessingDocument Document
Methods
public IEnumerable<Blip> GetAllImages()
Mengambil keseluruhan foto yang berada di dalam dokumen Wordpublic void ReplaceImage(Blip blipElement)
Menggati foto dengan hasil modifikasi fotopublic void Dispose()
Menghancurkan objek dari memori
Events
ImageChanging(ref Image image)
Bangkit ketika foto sedang digantiImageChanged(object sender, EventArgs arg)
Bangkit ketika foto telah diganti
Bagaimana Melakukannya
- Download berkas DLL-nya
- Tambahkan ke "References" di project anda
- Buat objek
WordImage
WordImageProcessing.WordImage wip = new WordImageProcessing.WordImage("example.docx");
Gunakan kode ini jika di dalam kode project anda telah ada objek
WordprocessingDocument
WordImageProcessing.WordImage wip = new WordImageProcessing.WordImage(wpd);
- Tambahkan event ImageChanging
wip.ImagesChanging += wip_ImagesChanging;
- Baca semua foto yang berada di dalam dokumen Word
foreach (var blip in wip.GetAllImages()) { }
- Ganti foto
foreach (var blip in wip.GetAllImages()) { wip.ReplaceImage(blip); }
- Hancurkan objek dari memori
wip.Dispose();
Semua Kode Digabung Menjadi Satu
WordImageProcessing.WordImage wip = new WordImageProcessing.WordImage(wpd); wip.ImagesChanging += wip_ImagesChanging; foreach (var blip in wip.GetAllImages()) { wip.ReplaceImage(blip); } wip.Dispose();
ImageChanging Event Handler
private void wip_ImagesChanging(ref Image image) { // You can write image processing code here }
Contoh Kode
Kode di bawah ini akan mengubah warna foto menjadi skala abu-abu (grayscale) pada setiap foto yang berada di dalam dokumen Word.
private void wip_ImagesChanging(ref Image image) { Bitmap newImage = (Bitmap)image; for (int x = 0; x < newImage.Width; x++) { for (int y = 0; y < newImage.Height; y++) { System.Drawing.Color oc = newImage.GetPixel(x, y); int grayScale = (int)((oc.R * 0.3) + (oc.G * 0.59) + (oc.B * 0.11)); System.Drawing.Color nc = System.Drawing.Color.FromArgb(oc.A, grayScale, grayScale, grayScale); newImage.SetPixel(x, y, nc); } } image = newImage; }
Download DLL
Jika ada pertanyaan tentang DLL ini, silahkan kirimkan melalui form komentar.
Tidak ada komentar:
Posting Komentar