Welcome, Guest. Please login or register.
Did you miss your activation email?
Pages: [1]   Go Down
  Print  
Author Topic: Save a scaled image taken from the screen to disk  (Read 821 times)
0 Members and 1 Guest are viewing this topic.
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2593
Referrals: 13

WWW Awards
« on: March 22, 2009, 06:57:38 PM »

Code
GeSHi (java):
  1. /*--------------------------------------------------------------------------
  2. CustomImage class
  3.  *****************
  4. By: deAppel http://www.engineeringserver.com
  5. Contact: info [@] engineeringserver.com
  6. Version: 22/03/2009
  7. "*****************
  8. Note: saves a scaled image taken from the screen to disk.
  9. part of the client / server remote desktop class by HappyFace. (Simplified demo)
  10. //----------------------------------------------------------------------*/
  11. import java.awt.AlphaComposite;
  12. import java.awt.Color;
  13. import java.awt.Dimension;
  14. import java.awt.Font;
  15. import java.awt.Graphics;
  16. import java.awt.Graphics2D;
  17. import java.awt.GraphicsConfiguration;
  18. import java.awt.GraphicsDevice;
  19. import java.awt.GraphicsEnvironment;
  20. import java.awt.Image;
  21. import java.awt.Rectangle;
  22. import java.awt.Robot;
  23. import java.awt.geom.AffineTransform;
  24. import java.awt.image.BufferedImage;
  25. import java.awt.image.RenderedImage;
  26. import java.io.ByteArrayOutputStream;
  27. import java.io.File;
  28. import java.io.FileInputStream;
  29. import java.io.FileOutputStream;
  30. import java.io.IOException;
  31. import java.io.InputStream;
  32. import java.io.ObjectInputStream;
  33. import java.net.BindException;
  34. import java.net.ConnectException;
  35. import java.net.ServerSocket;
  36. import java.net.Socket;
  37. import java.text.SimpleDateFormat;
  38. import java.util.ArrayList;
  39. import java.util.Calendar;
  40. import java.util.Date;
  41. import java.util.Vector;
  42. import java.util.zip.DataFormatException;
  43. import java.util.zip.GZIPInputStream;
  44. import java.util.zip.GZIPOutputStream;
  45. import java.util.zip.Inflater;
  46.  
  47. import javax.imageio.IIOException;
  48. import javax.imageio.ImageIO;
  49. import javax.swing.ImageIcon;
  50. import javax.swing.JLabel;
  51. import javax.swing.JScrollPane;
  52. import javax.swing.SwingWorker;
  53. import java.awt.*;
  54. import java.awt.event.*;
  55. import javax.swing.*;
  56.  
  57. //import com.sun.xml.internal.messaging.saaj.util.ByteInputStream;
  58.  
  59. import java.net.*;
  60. import java.io.*;
  61.  
  62. public class CustomImage extends Thread{
  63. ImageIcon img;
  64. static String imageExtention = "gif";
  65. BufferedImage bImage;
  66.  
  67. public CustomImage(){}
  68. public static void main(String args[]) {
  69. try{
  70. Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
  71. Rectangle screenRectangle = new Rectangle(screenSize);
  72. Robot robot = new Robot();
  73. BufferedImage bImg = robot.createScreenCapture(screenRectangle);
  74.  
  75. int width = 640;
  76. int height = 480;
  77.  
  78. Image image = bImg.getScaledInstance(width, height,
  79. bImg.SCALE_SMOOTH);
  80. bImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  81.  
  82. // Create the buffered image
  83. GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  84. GraphicsDevice gs = ge.getDefaultScreenDevice();
  85. GraphicsConfiguration gc = gs.getDefaultConfiguration();
  86. BufferedImage bimg = gc.createCompatibleImage(
  87. bImg.getWidth(null), bImg.getHeight(null), 1);
  88.  
  89. Graphics g = bimg.createGraphics();
  90. g.drawImage(image, 0, 0, null);
  91. g.setFont(new Font("Serif", Font.BOLD + Font.ITALIC, 40));
  92. g.setColor(Color.BLACK);
  93. g.drawString("Test - www.engineeringserver.com", 20, 40);
  94. g.dispose();
  95.  
  96. File f = new File("c:\\test." + imageExtention);
  97. if(!f.exists()){
  98. f.createNewFile();
  99. }
  100. ImageIO.write( bimg, imageExtention, f);
  101. }
  102. catch(Exception e){}
  103. }
  104. }
  105.  
  106.  
Created by GeSHI 1.0.7.20
Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
Pages: [1]   Go Up
  Print  
 
Jump to:  

Your Ad Here