Posts Tagged ‘csharp’

C#: generate image thumbnail

Wednesday, August 11th, 2010

//create the thumbnail 
int image_width = 96; 
int image_height = 96; 
Image imgThumbnail = imgOriginal.GetThumbnailImage(image_width, image_height, null, IntPtr.Zero); 
//save to file 
imgThumbnail.Save(Path.GetDirectoryName(AFileName) + @"\a2.bmp", ImageFormat.Bmp);


        private bool ThumbnailCallback()
            return false;
        private void ProcessImage(Image imgOriginal)
            //create the thumbnail
            int image_width = 96;
            int image_height = 96;
            Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
            Image imgThumbnail = imgOriginal.GetThumbnailImage(image_width, image_height, myCallback, IntPtr.Zero);
            //save to files
            imgThumbnail.Save(Path.GetDirectoryName(AFileName) + @"\a2.png");

C#: random file name

Wednesday, August 11th, 2010
string path = Path.GetRandomFileName();

C#: password characters for property in Property Grid

Wednesday, August 11th, 2010

To not show your passwords in plain text on a propertygrid, just use the PasswordPropertyText attribute:

[DisplayName("Password"), Category("Authentication"), PasswordPropertyText(true)] 
public string Password 

C#: image to byte array

Wednesday, August 11th, 2010
    private byte[] ImageToByteArray(Image img) 
        // stream to save the image to byte array 
        Stream ms = new MemoryStream(); 
        img.Save(ms, img.RawFormat); 
        // read to end 
        byte[] imgBytes = new byte[ms.Length-1]; 
        ms.Position = 0; 
        ms.Read(imgBytes, 0, imgBytes.Length); 
        return imgBytes; 

C#: получить имя юнита и номер строки с ошибкой

Friday, July 9th, 2010

Если в сборку включена отладочная информация, то:

string currentFile = new System.Diagnostics.StackTrace(true).GetFrame(0).GetFileName(); 
int currentLine = new System.Diagnostics.StackTrace(true).GetFrame(0).GetFileLineNumber();

C#: convert document to PDF

Tuesday, May 18th, 2010

Today I wrote small code to convert the document (.doc, .rtf etc) ionto PDF format using MS Word 12/2007 automation:

using Word = Microsoft.Office.Interop.Word;
private void SaveRTFToPDF(string RTFFileName, string PDFFileName)
			object oMissing = System.Reflection.Missing.Value;
			Object oTrue = true;
			Object oFalse = false;
			object ReadOnly = false;
			object IsVisible = true;
			object RTFFile = RTFFileName;
			object PDFFile = PDFFileName;
			Word._Application oWord = new Word.Application();
			Word._Document oDoc = oWord.Documents.Open(ref RTFFile,
				ref oMissing, ref ReadOnly,
			ref oMissing, ref oMissing,
				ref oMissing, ref oMissing,
				ref oMissing, ref oMissing,
				ref oMissing, ref oMissing,
				ref IsVisible, ref oMissing,
				ref oMissing, ref oMissing,
				ref oMissing);
			object FileFormat = Word.WdSaveFormat.wdFormatPDF;
			object LockComments = false;
			object AddToRecentFiles = true;
			object ReadOnlyRecommended = false;
			object EmbedTrueTypeFonts = false;
			object SaveNativePictureFormat = true;
			object SaveFormsData = true;
			object SaveAsAOCELetter = false;
			object InsertLineBreaks = false;
			object AllowSubstitutions = false;
			object LineEnding = Word.WdLineEndingType.wdCRLF;
			object AddBiDiMarks = false;
			oDoc.SaveAs(ref PDFFile, ref FileFormat, ref LockComments,
				ref oMissing, ref AddToRecentFiles, ref oMissing,
				ref ReadOnlyRecommended, ref EmbedTrueTypeFonts,
				ref SaveNativePictureFormat, ref SaveFormsData,
				ref SaveAsAOCELetter, ref oMissing, ref InsertLineBreaks,
				ref AllowSubstitutions, ref LineEnding, ref AddBiDiMarks);
			oDoc.Close(ref oFalse, ref oMissing, ref oMissing);
			oWord.Quit(ref oMissing, ref oMissing, ref oMissing);

Sample to use:

			SaveRTFToPDF(@"d:\C#\rtf2pdf\rtf\10-052561_1.RTF", @"d:\C#\rtf2pdf\rtf\10-052561_1.pdf");
			SaveRTFToPDF(@"d:\C#\rtf2pdf\rtf\10-052561_2.RTF", @"d:\C#\rtf2pdf\rtf\10-052561_2.pdf");
			SaveRTFToPDF(@"d:\C#\rtf2pdf\rtf\10-072410.RTF", @"d:\C#\rtf2pdf\rtf\10-072410.pdf");
			SaveRTFToPDF(@"d:\C#\rtf2pdf\rtf\10-080976.RTF", @"d:\C#\rtf2pdf\rtf\10-080976.pdf");
			SaveRTFToPDF(@"d:\C#\rtf2pdf\rtf\10-087077.RTF", @"d:\C#\rtf2pdf\rtf\10-087077.pdf");

I tried to convert the rtf into PDF using Crystal Reports. Just to generate the report form on fly from code and print there the RichTextBox.
But unfortunatelly only simple rtf-files exported as PDF in such way because Crystal Reports supports the limited syntax of rtf-format.
For example, header/footer of rtf-files is not converted correctly and images are not saved at all.

Viewer for Crystal Reports (*.rpt)

Wednesday, March 31st, 2010

Today I wrote for Ethan the small tool – viewer for .rpt files.
User need select the directory with saved report forms, specify the logon info for MS SQL and view/save as pdf the any report:

Main form for rptview

There I used the C# because viewer requires the Crystal Reports runtime library and I have the merge modules (for setup/install) only in MS Visual Studio 2003. Didn’t find in CodeGear/Embacadero disks.
The setup (.msi) created in MS Visual Studio 2003 too

C#: enable XP theming for application

Wednesday, March 31st, 2010

1. use the manifest (compile as resource or as external file)
2. call the EnableVisualStyles method for Application in Main():

 	static void Main() 
 		Application.Run(new frmMain()); 

C#: convert the byte array to string

Wednesday, December 16th, 2009
byte[] arrData = new byte[myLength];
Stream.Read(arrData, 0, myLength);
String s = System.Text.ASCIIEncoding.ASCII.GetString(arrData);

C#: read the number from buffer

Wednesday, December 16th, 2009

1. read from stream:

  byte[] arrData = {0, 0, 0, 0, 0, 0, 0xF0, 0xBF}; 
  Double dbl; 
  MemoryStream ms = new MemoryStream(); 
  ms.Write(arrData, 0, 8); 
  ms.Position = 0; 
  BinaryReader br = new BinaryReader(ms); 
  dbl = br.ReadDouble();

2. copy/convert from buffer:

  dbl = BitConverter.ToDouble(arrData, 0);