About Me

My photo
Northglenn, Colorado, United States
I'm primarily a BI Developer on the Microsoft stack. I do sometimes touch upon other Microsoft stacks ( web development, application development, and sql server development).
Showing posts with label convert. Show all posts
Showing posts with label convert. Show all posts

Tuesday, December 15, 2009

Converting XPS to Bitmap

One thing I had to do recently, for a SSRS report, was attach a 3 page pdf to a notice that is mailed out. Since, I can't just attach a pdf directly to the report, I figured I'll just take the images.

So, first thing I did was print the pdf to a XPS file. Now I just need to take that XPS file and convert it to some type of usable image that SSRS can recognize.

Found a solution on one of the MSDN message boards; however, I had to make some updates to it:

Code Snippet
  1. static public void SaveXpsPageToBitmap(string xpsFileName)
  2. {
  3.     DirectoryInfo di = new DirectoryInfo(xpsFileName);
  4.     XpsDocument xpsDoc = new XpsDocument(xpsFileName, System.IO.FileAccess.Read);
  5.     FixedDocumentSequence docSeq = xpsDoc.GetFixedDocumentSequence();
  6.  
  7.     //DocumentReferenceCollection drc = docSeq.References;
  8.     for (int i = 0; i < docSeq.DocumentPaginator.PageCount; i++)
  9.     {
  10.         DocumentPage docPage = docSeq.DocumentPaginator.GetPage(i);
  11.         BitmapImage bitmap = new BitmapImage();
  12.         RenderTargetBitmap renderTarget = new RenderTargetBitmap((int)docPage.Size.Width, (int)docPage.Size.Height, 96, 96, PixelFormats.Default);
  13.     
  14.         renderTarget.Render(docPage.Visual);
  15.  
  16.         BitmapEncoder encoder = new BmpBitmapEncoder();
  17.         encoder.Frames.Add(BitmapFrame.Create(renderTarget));
  18.  
  19.         FileStream pageOutStream = new FileStream(di.FullName.Substring(0,di.FullName.Length - 4) + "_Page_" + (i+1).ToString() + ".bmp", FileMode.Create, FileAccess.Write);
  20.         encoder.Save(pageOutStream);
  21.         pageOutStream.Close();
  22.     }
  23. }




So this program works nicely, only problem I currently have is that I have to convert these large size bitmaps (~5MB per page) to something more reasonable. Especially, since the SSRS deployment gives me a SOAP error because of the size. I'll probably just end up converting them to Jpeg later on.

Tuesday, October 02, 2007

Visual Studio 2005 Add-In: Code to HTML

Ok, this is a very cool tool and time saving add-in for visual studio 2005. Just highlight the code you want to copy to html, right click > copy to html. It does the formatting, color scheme, etc..

http://www.jtleigh.com/people/colin/software/CopySourceAsHtml/




1 class WaterMarkToolTip : ToolTip


2 {


3 public WaterMarkToolTip()


4 {


5 this.OwnerDraw = true;


6 this.Draw += new DrawToolTipEventHandler(OnDraw);


7


8 }


9


10 public WaterMarkToolTip(System.ComponentModel.IContainer Cont)


11 {


12 this.OwnerDraw = true;


13 this.Draw += new DrawToolTipEventHandler(OnDraw);


14 }


15


16 private void OnDraw(object sender, DrawToolTipEventArgs e)


17 {


18 DrawToolTipEventArgs newArgs = new DrawToolTipEventArgs(e.Graphics,


19 e.AssociatedWindow, e.AssociatedControl, e.Bounds, e.ToolTipText,


20 this.BackColor, this.ForeColor, new Font("Arial Unicode MS", 8.25f, FontStyle.Bold));


21 newArgs.DrawBackground();


22 newArgs.DrawBorder();


23 newArgs.DrawText(TextFormatFlags.TextBoxControl);


24 }


25


26


27 }


Monday, October 01, 2007

Reminder: TSQL date only

Get the date portion of datetime, correctly:

SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))


since,

CONVERT(VARCHAR(20),GETDATE(),112) returns the wrong date sometimes.

-----A cool and faster way:


DECLARE @datemask bigint
DECLARE @timemask bigint

SET @datemask = 0xffffffffff000000
SET @timemask = 0x0000000000ffffff

SELECT CAST(CAST((CAST(getdate() as binary) & @datemask) as binary(8)) as datetime)



//IF you need to grab a date and a time from two different locations in the database and combine them into one datetime (ex: shifts(time) and payperiod(date)):

SELECT CAST(CAST((CAST(getdate() as binary) & (@datemask | (CAST((CAST(getdate() as binary) & (@timemask) ) as binary(8)))) ) as binary(8)) as datetime)

Friday, September 28, 2007

Some steps when converting Dynamic tsql to Static

Here is a list of things to keep in mind when converting dynamic tsql stored procedures into a static version (for performance enhancement).

1) Remove quotes (carefully). - Going through and eliminated the outside quotes is the easy part. You do have to be careful of the single inner quotes, for example a single quote(s) might be necessary in a varchar statement.

2) Look over the convert statements. - A majority of the convert statements, especially the converting to varchar will need to be looked over and possibly removed in most cases.

3) Replace variables that dealt with parsing. - Some stored procedures might be sending in multiple data into one variable for example @Y = ('1,2,3,4,5') and being accessed like: WHERE x in @Y. This will need to be replaced with either a PATINDEX or CHARINDEX.

4) Remove unnecessary declarations - Some variables are going to end up not being used now, for example the varchar variable that was used to make the dynamic tsql string.



These are the tips, I might remember some more and add them later.