code_junkie

gdi+ Graphics::DrawImage really slow~~

Nov 12th, 2011
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. Bitmap *bitmap = Bitmap::FromFile("XXXX",...);
  2.  
  3. int width = bitmap->GetWidth();
  4. int height = bitmap->GetHeight();
  5. DrawImage(bitmap,0,0,width,height);
  6.  
  7. SetStretchBltMode(hDC, COLORONCOLOR);
  8. StretchDIBits(hDC, rcDest.left, rcDest.top,
  9. rcDest.right-rcDest.left, rcDest.bottom-rcDest.top,
  10. 0, 0, width, height,
  11. BYTE* dib, dibinfo, DIB_RGB_COLORS, SRCCOPY);
  12.  
  13. Bitmap bmp = new Bitmap(...)
  14. TextureBrush myBrush = new TextureBrush(bmp)
  15.  
  16. private void Paint(object sender, PaintEventArgs e):
  17. {
  18. //Don't draw the bitmap directly.
  19. //Only draw TextureBrush inside the Paint event.
  20. e.Graphics.FillRectangle(myBrush, ...)
  21. }
  22.  
  23. DrawImage(bitmap,0,0);
  24.  
  25. DrawImage(bitmap, 0, 0, bitmap.GetWidth, bitmap.GetHeight);
  26.  
  27. class procedure TGDIPlusHelper.DrawCachedBitmap(image: TGPImage;
  28. var cachedBitmap: TGPCachedBitmap;
  29. Graphics: TGPGraphics; x, y: Integer; width, height: Integer);
  30. var
  31. b: TGPBitmap;
  32. begin
  33. if (image = nil) then
  34. begin
  35. //i've chosen to not throw exceptions during paint code - it gets very nasty
  36. Exit;
  37. end;
  38.  
  39. if (graphics = nil) then
  40. begin
  41. //i've chosen to not throw exceptions during paint code - it gets very nasty
  42. Exit;
  43. end;
  44.  
  45. //Check if we have to invalidate the cached image because of size mismatch
  46. //i.e. if the user has "zoomed" the UI
  47. if (CachedBitmap <> nil) then
  48. begin
  49. if (CachedBitmap.BitmapWidth <> width) or (CachedBitmap.BitmapHeight <> height) then
  50. FreeAndNil(CachedBitmap); //nil'ing it will force it to be re-created down below
  51. end;
  52.  
  53. //Check if we need to create the "cached" version of the bitmap
  54. if CachedBitmap = nil then
  55. begin
  56. b := TGDIPlusHelper.ResizeImage(image, width, height);
  57. try
  58. CachedBitmap := TGPCachedBitmap.Create(b, graphics);
  59. finally
  60. b.Free;
  61. end;
  62. end;
  63.  
  64. if (graphics.DrawCachedBitmap(cachedBitmap, x, y) <> Ok) then
  65. begin
  66. //The calls to DrawCachedBitmap failed
  67. //The API is telling us we have to recreate the cached bitmap
  68. FreeAndNil(cachedBitmap);
  69. b := TGDIPlusHelper.ResizeImage(image, width, height);
  70. try
  71. CachedBitmap := TGPCachedBitmap.Create(b, graphics);
  72. finally
  73. b.Free;
  74. end;
  75.  
  76. graphics.DrawCachedBitmap(cachedBitmap, x, y);
  77. end;
  78. end;
  79.  
  80. Image imgPrintInvoice = new Image.FromFile("printer.png");
  81. CachedBitmap imgPrintInvoiceCached = null;
  82.  
  83. ...
  84.  
  85. int glyphSize = 16 * (GetCurrentDpi() / 96);
  86.  
  87. DrawCachedBitmap(imgPrintInvoice , ref imgPrintInvoiceCached , graphics,
  88. 0, 0, glyphSize, glyphSize);
Add Comment
Please, Sign In to add comment