Friday, April 29, 2011

What is the fastest way to draw an Image on another image?

I have 3 Bitmap point .

Bitmap* totalCanvas = new Bitmap(400, 300, PixelFormat32bppARGB);  // final canvas
Bitmap* bottomLayer  = new Bitmap(400, 300,PixelFormat32bppARGB); // background
Bitmap* topLayer = new Bitmap(XXX); // always changed.

I will draw complex background on bottomLayer. I don't want to redraw complex background on totalCanvas again and again, so I stored it in bottomLayer.

TopLayer changed frequently. I want to draw bottomLayer to totalCanvas. Which is the fastest way?

Graphics canvas(totalCanvas); 
canvas.DrawImage(bottomLayer, 0, 0);  step1
canvas.DrawImage(topLayer ,XXXXX);   step2

I want step1 to be as fast as possible. Can anyone give me some sample? Thanks very much!

Thanks for unwind's answer. I write the following code:

Graphics canvas(totalCanvas); 
for (int i = 0; i < 100; ++i)
{
canvas.DrawImage(bottomLayer, 0,0);
}

this part takes 968ms... it is too slow...

From stackoverflow
  • Almost all GDI+ operations should be implemented by the driver to run as much as possible on the GPU. This should mean that a simple 2D bitmap copy operation is going to be "fast enough", for even quite large values of "enough".

    My recommendation is the obvious one: don't sweat it by spending time hunting for a "fastest" way of doing this. You have formulated the problem very clearly, so just try implementing it that clearly, by doing it as you've outlined in the question. Then you can of course go ahead and benchmark it and decide to continue the hunt.

    A simple illustration:

    A 32 bpp 400x300 bitmap is about 469 KB in size. According to this handy table, an Nvidia GeForce 4 MX from 2002 has a theoretical memory bandwidth of 2.6 GB/s. Assuming the copy is done in pure "overwrite" mode, i.e. no blending of the existing surface (which sounds right, as your copy is basically a way of "clearing" the frame to the copy's source data), and an overhead factor of four just to be safe, we get:

    (2.6 * 2^30 / (4 * 469 * 2^10)) = 1453

    This means your copy should run at 1453 FPS, which I happily assume to be "good enough".

    : Thanks!, I tried but it seems not fast enough...
  • If at all possible (and it looks like it from your code), using DrawImageUnscaled will be significgantly faster than DrawImage. Or if you are using the same image over and over again, create a TextureBrush and use that.

    The problem with GDI+, is that for the most part, it is unaccelerated. To get the lightening fast drawing speeds you really need GDI and BitBlt, which is a serious pain in the but to use with GDI+, especially if you are in Managed code (hard to tell if you are using managed C++ or straight C++).

    See this post for more information about graphics quickly in .net.

Adobe PDF x64 ifilter

Getting a weird error box when using the 64bit version of adobepdf ifilter for sql server 2005 x64 on a x64 windows server 2003.

The exact message box contents:

MessageBoxHeader: MsFTEFD.exe - Unable to locate component

MessageboxContent: This application has failed to start because adobepdfl.dll was not found. Re-installing the application may fix the problem.

Has anyone an idea what to do here? Re-installation of the ifilter did not solved the problem (including the steps needed to register the filter correctly to work with SQL server), Google does not pop up with any solution. Checked the application & system logs with no success.

From stackoverflow
  • This is not really programming related. However, I would look into whether you are running 64 bit SQL server, and whether the filter plugin is 64 or 32 bit.

    Greco : well, programming in terms of sql I would say -> full-text-search over pdf files is working correctly or not with having this error message box being displayed.

Entity Sql for a Many to Many relationship

Consider two tables Bill and Product with a many to many relationship. How do you get all the bills for a particular product using Entity Sql?

From stackoverflow
  • You need to use some linq like this;

    ...
    using (YourEntities ye = new YourEntities())
    {
       Product myProduct = ye.Product.First(p => p.ProductId = idParameter);
       var bills = myProduct.Bill.Load();       
    }
    ...
    

    This assumes that you have used the entitiy framework to build a model for you data. The bills variable will hold a collection of Bill objects that are related to your product object.

    Hope it helps.

  • Something like this

    SELECT B FROM [Container].Products as P
    OUTER APPLY P.Bills AS B
    WHERE P.ProductID == 1
    

    will produce a row for each Bill

    Another option is something like this:

    SELECT P, (SELECT B FROM P.Bills)
    FROM [Container].Products AS P
    WHERE P.ProductID == 1
    

    Which will produce a row for each matching Product (in this case just one) and the second column in the row will include a nested result set containing the bills for that product.

    Hope this helps

    Alex

  • SELECT VALUE b 
    FROM Bills AS b 
    WHERE b.Product.Id == 1
    

How to display a datagrid in a new window (using callprint) but with CSS formatting?

Datagrid displays in new window with the javascript function callprint but losers all css formatting. Is there a way to fix this?

From stackoverflow
  • Either

    • Don't call the datagrid to display in new window.

    Or

    • Create a page which contains the datagrid and contains the CSS and use javascript to open this window.

SELECT * FROM in MySQLi

My site is rather extensive, and I just recently made the switch to PHP5 (call me a late bloomer).

All of my MySQL query's before were built as such:

"SELECT * FROM tablename WHERE field1 = 'value' && field2 = 'value2'";

This made it very easy, simple and friendly.

I am now trying to make the switch to mysqli for obvious security reasons, and I am having a hard time figuring out how to implement the same SELECT * FROM queries when the bind_param requires specific arguments.

Is this statement a thing of the past?

If it is, how do I handle a query with tons of columns involved? Do I really need to type them all out every time?

From stackoverflow
  • Is this statement a thing of the past?

    Yes. Don't use SELECT *; it's a maintenance nightmare. There are tons of other threads on SO about why this construct is bad, and how avoiding it will help you write better queries.

    See also:

    johnnietheblack : okay...i totally believe you, but im having trouble finding them somehow, and i could learn from them im sure. coudl you give me a link? thanks:)
    Thilo : I think the question was about bind variables. I see no big problem with "SELECT *" (if you need more than just primary key or otherwise indexed columns).
    kquinn : Yeah, search engines don't like querying for "SELECT *" much; I edited my post with a couple links (the first link is especially useful).
    johnnietheblack : kquinn - thanks for the resources:)
  • You can still use it (mysqli is just another way of communicating with the server, the SQL language itself is expanded, not changed). Prepared statements are safer, though - since you don't need to go through the trouble of properly escaping your values each time. You can leave them as they were, if you want to but the risk of sql piggybacking is reduced if you switch.

    soulmerge : Sometimes you even *need* the old usage. Mysql can't do multiple inserts in a prepared statement for example.
    johnnietheblack : it cant? isn't that something that most people need to do?
    soulmerge : No, it cannot do something like INSERT INTO A (a,b) VALUES (1,2), (3,4), (5,6). And *if* (and only if) speed is an issue (which shouldn't be in 99% of cases - but we just had one in a recent project), consider this: prepared statements need 3 communications for 1 query (prepare, set var, execute)
    johnnietheblack : oh, i gotcha....makes sense now
  • "SELECT * FROM tablename WHERE field1 = 'value' && field2 = 'value2'";
    

    becomes

    "SELECT * FROM tablename WHERE field1 = ? && field2 = ?";

    which is passed to the $mysqli::prepare:

    $stmt = $mysqli->prepare(
      "SELECT * FROM tablename WHERE field1 = ? && field2 = ?");
    $stmt->bind_param( "ss", $value, $value2); 
    // "ss' is a format string, each "s" means string
    $stmt->execute();
    
    $stmt->bind_result($col1, $col2);
    // then fetch and close the statement
    

    OP comments:

    so if i have 5 parameters, i could potentially have "sssis" or something (depending on the types of inputs?)

    Right, one type specifier per ? parameter in the prepared statement, all of them positional (first specifier applies to first ? which is replaced by first actual parameter (which is the second parameter to bind_param)).

    mysqli will take care of escaping and quoting (I think).

    johnnietheblack : so if i have 5 parameters, i could potentially have "sssis" or something (depending on the types of inputs?)
  • While you are switching, switch to PDO instead of mysqli, It helps you write database agnositc code and have better features for prepared statements.

    http://www.php.net/pdo

    Bindparam for PDO: http://se.php.net/manual/en/pdostatement.bindparam.php

    $sth = $dbh->prepare("SELECT * FROM tablename WHERE field1 = :value1 && field2 = :value2");
    $sth->bindParam(':value1', 'foo');
    $sth->bindParam(':value2', 'bar');
    $sth->execute();
    

    or:

    $sth = $dbh->prepare("SELECT * FROM tablename WHERE field1 = ? && field2 = ?");
    $sth->bindParam(1, 'foo');
    $sth->bindParam(2, 'bar');
    $sth->execute();
    

    or execute with the parameters as an array:

    $sth = $dbh->prepare("SELECT * FROM tablename WHERE field1 = :value1 && field2 = :value2");
    $sth->execute(array(':value1' => 'foo' , ':value2' => 'bar'));
    

    It will be easier for you if you would like your application to be able to run on different databases in the future.

    I also think you should invest some time in using some of the classes from Zend Framwework whilst working with PDO. Check out their Zend_Db and more specifically [Zend_Db_Factory][2]. You do not have to use all of the framework or convert your application to the MVC pattern, but using the framework and reading up on it is time well spent.

    johnnietheblack : awesome, iv never heard of PDO, ill check it out
  • This was already a month ago, but oh well.

    I could be wrong, but for your question I get the feeling that bind_param isn't really the problem here. You always need to define some conditions, be it directly in the query string itself, of using bind_param to set the ? placeholders. That's not really an issue.

    The problem I had using MySQLi SELECT * queries is the bind_result part. That's where it gets interesting. I came across this post from Jeffrey Way: http://jeff-way.com/2009/05/27/tricky-prepared-statements/. The script basically loops through the results and returns them as an array — no need to know how many columns there are, and you can still use prepared statements.

    In this case it would look something like this:

    $stmt = $mysqli->prepare(
      'SELECT * FROM tablename WHERE field1 = ? AND field2 = ?');
    $stmt->bind_param('ss', $value, $value2);
    $stmt->execute();

    Then use the snippet from the site:

    $meta = $stmt->result_metadata();
    
    while ($field = $meta->fetch_field()) {
      $parameters[] = &$row[$field->name];
    }
    
    call_user_func_array(array($stmt, 'bind_result'), $parameters);
    
    while ($stmt->fetch()) {
      foreach($row as $key => $val) {
        $x[$key] = $val;
      }
      $results[] = $x;
    }

    And $results now contains all the info from SELECT *. So far I found this to be an ideal solution.

    0plus1 : Thanks, this was the exact same problem I had, the accepted answer won't work with SELECT *

REF CURSOR versus TABLE function in Oracle

Hi,

I have PL-SQL Packages which return REF Cursors when data has to be retrieved as part of the CRUD operations. Would it be faster if these cursors were replaced with TABLE functions ?

Thanks

From stackoverflow
  • IMO TABLE functions is more usefull if you planned to use pipelined table functions. It`s not be faster, because REF is only reference to a memory. And all the work (parse, execute, fetch and etc.) will be processed out of the function that return REF Cursor. REF Cursors adds flexibility to the detriment of ease of support. That is another article from "MacLochlainns Weblog" about REF Cursors and pipelined functions - Reference Cursors - Why, when, and how?

How to transform huge XML files into relational data to perform queries

I have at least 100 xml files each about 300 MB with email messages basically in the format listed below.

Now my question is, how do I get this data into say SQL Sever database so that I can perform query on this data. My queries would be along the lines of: Has a certain person sent an email to another certain person on a given period with certain keywords on subject/body etc.

Here is what I have tried:

1) Loading each XML file into XML data type field into SQL Server. With this approach I could not come up with the Xpath(?) queries to do what I need. Is it even possible to do this in Xpath?

2) Loading each file into .NET DataSet using ReadXML and ReadSchema. This seems to Load fine and it seems to create the right number of DataTable with the foreign keys etc but this would mean I will have to create 100 sets of table on the database. Somehow join all into one single table then perform the query.

Let me know if you guys have any other suggestions.

Thanks.

<Message>
<MsgID>4651286700000CAA00EF00010000</MsgID>
<MsgTime>2007-05-21-01.04.39.000000</MsgTime>
<MsgTimeUTC>1179723879</MsgTimeUTC>
<MsgLang>CODE 1252</MsgLang>
<Sender>
 <UserInfo>
  <FirstName>X</FirstName>
  <LastName>Y</LastName>
  <AccountName>121212</AccountName>
  <CorporateEmailAddress>someone@somewhere.com</CorporateEmailAddress>
 </UserInfo>
</Sender>
<Recipient DeliveryType = " ">
 <UserInfo>
  <FirstName>A</FirstName>
  <LastName>B</LastName>
  <FirmNumber>7593</FirmNumber>
  <AccountName>STRATEGIC AS</AccountName>
  <AccountNumber>604806</AccountNumber>
  <CorporateEmailAddress>A@B.COM</CorporateEmailAddress>
 </UserInfo>
</Recipient>
<Subject>
 Please review the following
</Subject>
<Attachment>
 <FileName>37715772.htm</FileName>
 <FileID>503242486522279_37715772.htm</FileID>
 <FileSize>31175</FileSize>
</Attachment>
<MsgBody>
 This is the message Body
</MsgBody>

From stackoverflow
  • Use the Xml Bulk Load component

    http://support.microsoft.com/kb/316005

    coderguy123 : don't know how I missed this. Thanks everyone who replied.
  • You could try using the XmlReader.ReadSubtree() to read one message at the time. Loading the whole file into memory is not a really good idea.

    Passing the data of one message to the SQL server is then easy, you can also pass the XML and parse it on the SQL server (using OPENXML) in a stored procedure.

  • If you are looking to do queries, why not just use XML LINQ to do this, instead. It would probably be simpler.

  • I'd use the ReadXML method you mentioned, but do it on one XML file at a time, and then use the DataSet/DataTable Merge method. As long as the schemas are the same it should just keep adding to the same tables (more rows instead of duplicate tables), and make sure you dispose of the XMLDocument or XMLTextReader that you had for that file each time, so you'll get the memory back.

    When you're all done, use a SqlDataAdapter and SqlCommandBuilder to auto-generate your INSERT statments and flush it to your SQL tables. This is easier if you use Visual Studio's Data Explorer to generate a strongly typed dataset.

    You may even want to do this intermittently and clear your dataset so you keep the memory down.

  • Use SQL Server Integration Services. There is an XML Source Adapter designed for this task, with a handy designer where you can select the nodes or attributes in your XML that will turn into columns in your database.

    See this blog post from the SSIS team.

  • If you need to do this once, I would create an xslt to transform the xml into SQL insert statements and execute the resulting sql script to populate the table(s). This won't give memory issues due to the large xml files.

  • It sounds like databasing is not a requirement, just a way of being able to query. If that is the case, try XMLMax, which should load your 300MB xml in about 20 seconds. It has an indexed xpath query, and indexing should take about 15 minutes. Other options would be a native xml database. eXistDB is opensource and I had one user tell me he has used it with gigabyte size xml and is very happy with it. But it does take a lot of time to load, index and get it set up for a given xml.