Archive

Author Archive

IFrames and the back button

If you have an Iframe on a page, and you press the back button, then oftentimes, only the Iframe goes back in it’s history, not the containing page.
 
There are a few workarounds for this, this is just a proposal, if it helps…
 
firstly, if you have a pre-loader on the iframe, such as…
 
graph.contentWindow.document.write(‘Please wait…’);
graph.src = ‘someurl’
 
when you can poll the Iframe for a change in the "Please wait message". (Note, that the url that you are redirecting to must be on the same domain as the parent page, or else you get a security exception)
 
// 951 – back button fix. 
 var wasGraphLoaded = false;
 setTimeout(‘checkGraph()’,500);
 function checkGraph()
 {
  if (imgGraph.document.body.innerHTML.indexOf("…")==-1)
  {
   // Graph must be loaded…
   wasGraphLoaded = true;  
   
  }
  else
  {
   // Graph isn’t loaded.
   if (wasGraphLoaded)
   {
    // The back button bust have been pressed
    window.history.go(-1);
   }
  }
  setTimeout(‘checkGraph()’,500);
 }
Categories: Uncategorized

Redistributing a VB6 application the “Free way”

After finding out that my WISE installer had passed it’s 30 day limit, and my Installshield CD scratched beyond repair, I looked on the web for a free way to redistribute a VB6 application.
 
My first attempt, was Advanced Installer 4, which looked promising. However, it also, is a 30 day trial, if you want go beyond the absolute basics. For instance, my app required the Soap Toolkit,(a COM object), so that was beyond the basic version. Also, it couldn’t properly self-register the COM DLL’s in the Advanced mode either. – Waste of time.
 
I then came accross a Microsoft tool, Visual Studio Installer, which looked well. With a bit of fiddling, I got it to create an MSI. However, an MSI may not work first time on pre-XP machines, since the Windows Installer is not guaranteed to be on the machine, So I looked for a generic MSI to EXE converter
 
The best I found was this: http://www.tmgdevelopment.co.uk/setuputility.htm Which you place in the same folder as the MSI.
 
I zipped them both together, and placed them on the website.
 
 
Categories: Uncategorized

Workaround for Bitmap ColorDepth in C#

I am almost cerain this is a bug in the .NET bitmap Codec, that you cannot change the color Depth… It always saves in its native format for Bitmap.
 
However, if you convert the image to a TIFF first, then it works.
 

Image imgTIFF = Image.FromStream(s);

ImageCodecInfo ici = GetEncoderInfo("TIFF");

EncoderParameter ep = new EncoderParameter(Encoder.ColorDepth,24L);

EncoderParameters eps =

new EncoderParameters(1);

eps.Param[0]= ep;

imgTIFF.Save(@"c:test.tiff",ici,eps);

Image imgBMP = Image.FromFile(@"c:test.tiff");

imgBMP.Save(@"c:test.bmp");

 
 
 
 
(for reference:)

private

ImageCodecInfo GetEncoderInfo(string mimeType)

{

ImageCodecInfo[] encs = ImageCodecInfo.GetImageEncoders();

for (int ix = 0; ix <= encs.Length; ix++)

{

if ( encs[ix].CodecName.IndexOf(mimeType) != -1)

{

return encs[ix];

}

}

return null;

}

Categories: Uncategorized

Converting a Bitmap to a pixel array

I was looking for some high performance code to convert a bitmap into a 2D byte array, this is what I came up with.
 
first I have a class called pixel.
public class pixel

{

  public byte red;

  public byte green;

  public byte blue;

}

 

then I read it using some Unsafe code thus:

public pixel[,] BitmapToArray(Bitmap b)

{

pixel[,] bmpData=new pixel[b.Width,b.Height];

// GDI+ still lies to us – the return format is BGR, NOT RGB.

BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

int stride = bmData.Stride;

System.IntPtr Scan0 = bmData.Scan0;

unsafe

{

byte * p = (byte *)(void *)Scan0;

int nOffset = stride – b.Width*3;

int nWidth = b.Width * 3;

for(int y=0;y<b.Height;++y)

{

for(int x=0; x < nWidth; ++x )

{

switch(x%3)

{

case 0:

bmpData[x/3,y] =

new pixel();

bmpData[x/3,y].blue = (

byte)(p[0]);

break;

case 1:

bmpData[x/3,y].green = (

byte)(p[0]);

break;

case 2:

bmpData[x/3,y].red = (byte)(p[0]);

break;

}

++p;

}

p += nOffset;

}

}

b.UnlockBits(bmData);

return bmpData;

}

 

 

Categories: Uncategorized

Converting a DataTable to CSV

This is a handly little C# function to convert a DataTable to CSV. Hope it’s useful..
 
public string DataTableToCSV(DataTable dt)
 {
 string strCSV = "";
 string strSeperator = "";
 foreach(DataColumn dc in dt.Columns)
 {
  strCSV += strSeperator + dc.ColumnName;
  strSeperator = ",";
 }
 strCSV += "rn";
 foreach(DataRow dr in dt.Rows)
 {
  strSeperator = "";
  foreach(DataColumn dc in dt.Columns)
  {
   strCSV += strSeperator + dr[dc.ColumnName].ToString();
   strSeperator = ",";
  }
  strCSV += "rn";
 }
 return strCSV;
 }
Categories: Uncategorized

SQL sever 2005 Management studio express

I’ve had SQL server 2005 installed on my server for some time now, and sucessfully running a few websites from it. I’ve had to work from the command line up to now, since, I couldn’t get any good GUI working.
 
I tried to download SQL server 2005 Management studio express, for which I had to upgrade the .NET framework on the server, upgrade MSXML, and then install the application.
 
Try to connect and….
 
===================================
Cannot connect to .SQLEXPRESS.
===================================
An error has occurred while establishing a connection to the server.  When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: SQL Network Interfaces, error: 26 – Error Locating Server/Instance Specified) (.Net SqlClient Data Provider)
——————————
Error Number: -1
Severity: 20
State: 0
——————————
Program Location:
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
   at System.Data.SqlClient.TdsParser.Connect(Boolean& useFailoverPartner, Boolean& failoverDemandDone, String host, String failoverPartner, String protocol, SqlInternalConnectionTds connHandler, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, SqlConnection owningObject, Boolean aliasLookup)
   at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance)
   at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance)
   at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection)
   at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup)
   at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
   at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
   at System.Data.SqlClient.SqlConnection.Open()
   at Microsoft.SqlServer.Management.UI.VSIntegration.ObjectExplorer.ObjectExplorer.ValidateConnection(UIConnectionInfo ci, IServerType server)
   at Microsoft.SqlServer.Management.UI.ConnectionDlg.Connector.ConnectionThreadUser()
 
 
Crap.
 
Any ideas?
 
Categories: Uncategorized

Resource files for Classic ASP

I was looking to localize a classic ASP website, and was considering upgrading it to ASP.NET, but it seemed way too much work, so I decided to look to see if it was possible to create a localization framework for classic ASP. using the same type of resx files that are used for asp.net
 
So, given a simple piece of xml in semi-typical resx format:
 
<root>
 <data name="Hello!">
  Ciao!
 </data>
</root>
 
I then wrote an include file in classic asp, to handle translations in the form
 
<%=Translate("Hello!")%>
 
(Precursed by a initResources("strings-it.xml"))
 
 
 Dim xmlResx
 Function initResources(ResourceFile)
  Set xmlResx=Server.CreateObject("Microsoft.XMLDOM")
  xmlResx.async=false
  xmlResx.load(Server.MapPath(ResourceFile))
 End Function
 
 Function Translate(english)
  Set objLst = xmlResx.getElementsByTagName("data")
  For i = 0 to (objLst.length -1)
    EnglishText = objLst.item(i).attributes.getNamedItem("name").text
 If english=EnglishText Then
  Translate= objLst.item(i).text
  Exit For
 End If
  Next
 End Function
 
20 Lines of code, prevented an overhaul of my application to .NET.
 
Ultimately, I plan this to be used for a spanish version of www.openmerchantaccount.com
 
 
 
 
Categories: Uncategorized

ASP.NET Crash on “OnBubbleEvent”

Categories: Uncategorized

Capturing a webcam to an AVI file

I previously wrote an application which captures a webcam in VB.NET (http://www.webtropy.com/articles/art7.asp) However, this only shows a webcam, and has no facilities to record. Therefore, I added a new function to save bitmaps
 

Public Sub SaveBitmap(ByVal filename As String)

SendMessage(lwndC, WM_CAP_FILE_SAVEDIB, 0&, filename)

End Sub

 

Where WM_CAP_FILE_SAVEDIB is defined as WM_USER+25

– It also requires an overloaded SendMessage declaration, which accepts a string as it’s final parameter.

 

I then found some CS code which converts BMP files to AVI (http://midget3d.com/gabe/AviLib.zip)

 

then used this to write bitmaps to disk, and build them into an AVI

 

saveFileDialog.Filter="*.avi|*.avi";

saveFileDialog.ShowDialog();

string strAVI = saveFileDialog.FileName;

string strBMP = saveFileDialog.FileName.Replace(".avi",".bmp");

AviWriter aviWriter =

new AviWriter(strAVI, AviCompression.None, 24, 320, 240);

isRecording =

true;

while(isRecording)

{

webcamControl1.SaveBitmap(strBMP);

aviWriter.WriteFrame(

new Bitmap(strBMP));

Application.DoEvents();

}

aviWriter.Close();

 
 
 
Categories: Uncategorized

Compress an AVI movie using C#

I was looking for a way to compress an AVI file down without loosing quality, and to do so programatically, in C#. All I found on the web, were commercial components. However, I just found out you can use QuickTime in C#, to compress the files down in size.
 

private bool ConvertToQuickTime(AxQTOControlLib.AxQTControl Control,string fromFileName,string toFileName)

{

this.axQTControl1.URL = fromFileName;

if (!axQTControl1.Movie.CanExport) return false;

QTOLibrary.QTQuickTime qt;

qt = axQTControl1.QuickTime;

if (qt.Exporters.Count == 0) qt.Exporters.Add();

QTOLibrary.QTExporter exp;

exp = qt.Exporters[1];

// Set exporter type: AVI, 3G, MPEG-4, PNG, etc.

exp.TypeName = "QuickTime Movie";

exp.SetDataSource(axQTControl1.Movie);

exp.DestinationFileName = toFileName;

exp.ShowProgressDialog =

true;

exp.BeginExport();

return true;

}

Categories: Uncategorized