How to Create a Microsoft Word Document with C#

object oMissing = System.Reflection.Missing.Value;

Microsoft.Office.Interop.Word.Application oWord;
oWord = new Microsoft.Office.Interop.Word.Application();
oWord.Visible = true;

Microsoft.Office.Interop.Word.Document oDoc;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);

// add text
Microsoft.Office.Interop.Word.Paragraph oPara1;
oPara1 = oDoc.Paragraphs.Add(ref oMissing);
object styleHeading1 = “Heading 1”;
oPara1.Range.set_Style(ref styleHeading1);
oPara1.Range.Text = “Create a New Report”;
oPara1.Range.InsertParagraphAfter();

Microsoft.Office.Interop.Word.Paragraph oPara2;
oPara2 = oDoc.Paragraphs.Add(ref oMissing);
oPara2.Range.Text = “”;
oPara2.Range.InsertParagraphAfter();

Microsoft.Office.Interop.Word.Paragraph oPara3;
oPara3 = oDoc.Paragraphs.Add(ref oMissing);
oPara3.Range.Text = “This is my second paragraph.”;
oPara3.Range.InsertParagraphAfter();

// add picture
string imgName = “c://tmp/doc1.jpg”;  //the picture file to be inserted
Object oMissed = oDoc.Paragraphs[2].Range; //the position you want to insert
Object oLinkToFile = false;  //default
Object oSaveWithDocument = true;//default
oDoc.InlineShapes.AddPicture(imgName, ref  oLinkToFile, ref  oSaveWithDocument, ref  oMissed);

// save
object fileName = “C:\\tmp\\test_report.doc”;
oDoc.SaveAs(ref fileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
oWord = null; // Free up any memory used

Word Object Model Overview
Ref: suite101.com

CSharp using C++ DLL

ในบทความนี้เป็นการใช้ C# เรียกใช้ DLL ที่เขียนจากภาษา C++

1. สร้างโปรเจ็กส์ C# เพื่อใช้งาน Dll ชื่อ CSConsole เป็นแบบ Console Application

2. สร้างโปรเจ็กส์ C# เพื่อใช้งาน Dll ชื่อ CSWinForm เป็นแบบ Windows Forms Application

เพิ่ม Code

using System;

เพิ่ม Code

using System;

ที่มา: Platform Invoke Tutorial

 

.NET Image Scanner

TWAIN

Windows Image Acquisition (WIA)

Convert Image to Icon

แปลงไฟล์ภาพ ไปเป็น icon มี 2 ตัวอย่าง ดังนี้

/// <summary>
/// Converts an image into an icon.
/// </summary>
/// <param name=”img”>The image that shall become an icon</param>
/// <param name=”size”>The width and height of the icon. Standard
/// sizes are 16×16, 32×32, 48×48, 64×64.</param>
/// <param name=”keepAspectRatio”>Whether the image should be squashed into a
/// square or whether whitespace should be put around it.</param>
/// <returns>An icon!!</returns>
private Icon MakeIcon(Image img, int size, bool keepAspectRatio) {
Bitmap square = new Bitmap(size, size); // create new bitmap
Graphics g = Graphics.FromImage(square); // allow drawing to it

int x, y, w, h; // dimensions for new image

if(!keepAspectRatio || img.Height == img.Width) {
// just fill the square
x = y = 0; // set x and y to 0
w = h = size; // set width and height to size
} else {
// work out the aspect ratio
float r = (float)img.Width / (float)img.Height;

// set dimensions accordingly to fit inside size^2 square
if(r > 1) { // w is bigger, so divide h by r
w = size;
h = (int)((float)size / r);
x = 0; y = (size – h) / 2; // center the image
} else { // h is bigger, so multiply w by r
w = (int)((float)size * r);
h = size;
y = 0; x = (size – w) / 2; // center the image
}
}

// make the image shrink nicely by using HighQualityBicubic mode
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(img, x, y, w, h); // draw image with specified dimensions
g.Flush(); // make sure all drawing operations complete before we get the icon

// following line would work directly on any image, but then
// it wouldn’t look as nice.
return Icon.FromHandle(square.GetHicon());

private void cmdConvert_Click(object sender, EventArgs e)
{
string source = @”d:\image.bmp”;
string target = @”d:\image.ico”;

BmpToIcon(source, target);
}

public static void BmpToIcon(string sourcePath, string
targetPath)
{

// Retrieve the bitmap from the file.
Bitmap bmp = (Bitmap)Image.FromFile(sourcePath);

// Convert the bitmap to an icon.
Icon ico = Icon.FromHandle(bmp.GetHicon());

// Create a file stream to save the icon stream.
Stream st = new FileStream(targetPath, FileMode.Create);

// Create a stream writer to physical write the data to
the disk.
BinaryWriter wr = new BinaryWriter(st);

// Write the binary icon data to the file stream.
ico.Save(st);

// Close the file to write the stream to the disk.
wr.Close();

ที่มา: dreamincode.net, bytes.com/topic/c-sharp

C# Visual Studio: Change the default icon of your .exe file.

To change the default icon of your .exe file click on :

  1. ไปที่ Project
  2. คลิ๊กขวาที่ Project เลือก Properties
  3. เลือก Application
  4. ที่ Icon ทำการเลือก icon ที่ต้องการ

เรียบร้อย

หรือจะใช้คำสั่ง ดังนี้
this.Icon = new System.Drawing.Icon(“shuttle.ico”);

ที่มา: normankosmal.comen.csharp-online.net

MDIList property missing?

การสร้างโปรเจ็กส์แบบ MDI
สิ่งสำคัญอย่างนึงคือการแสดงรายชื่อหน้าต่างย่อยบนเมนู Window ให้เลือกใช้ได้ง่ายๆ โดยการเขียน MDI ด้วย VS2003 ก็ใช้ MainMenu แล้วกำหนดคุณสมบัติ MDIList
แต่ตั้งแต่ VS2005 ไม่มี MainMenu ให้เลือกใช้ (แต่ถ้า Upgrade มาจาก VS2003 ก็ยังใช้ได้) ต้องเลือกใช้ MenuStrip แทน
แต่ MenuStrip ดันไม่มี MDIList property ให้ใช้
ทางแก้ก็ง่ายๆครับ

Me.MenuStrip1.MdiWindowListItem = WindowMenuToolStripItem

โดย WindowMenuToolStripItem คือ เมนูที่เราตั้งชื่อไว้ว่า Window ที่จะให้แสดงชื่อของหน้าต่างลูกนั่นเอง