Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 21-30 of 32 total

Java - ZoomIN Image

   1  
   2  public BufferedImage zoomIn(BufferedImage bi, int scale)
   3  	{
   4  		int width = scale * bi.getWidth();
   5  		int height = scale * bi.getHeight();
   6  		
   7  		BufferedImage biScale = new BufferedImage(width, height, bi.getType());
   8  		
   9                  // Cicla dando un valore medio al pixel corrispondente
  10  		for(int i=0; i<width; i++)
  11  			for(int j=0; j<height; j++)
  12  				biScale.setRGB(i, j, bi.getRGB(i/scale, j/scale));
  13  		
  14  		return biScale;
  15  	}

Java - imgFromJar

// Inserire un immagine presente nel file jar dell'applicazione

   1  
   2  try
   3  		{
   4  			icon = ImageIO.read(getClass().getResourceAsStream("imgs/icon.png")); // La preleva dal file jar
   5  			setIconImage(icon);
   6  		}
   7  		catch(IOException e)
   8  		{
   9  			e.printStackTrace();
  10  		}

Javascript - Rollover con preview immagine

// loader.js
   1  
   2  var t_id = setInterval(animate,20);
   3  var pos=0;
   4  var dir=2;
   5  var len=0;
   6  
   7  function animate()
   8  {
   9  var elem = document.getElementById('progress');
  10  if(elem != null) {
  11  if (pos==0) len += dir;
  12  if (len>32 || pos>79) pos += dir;
  13  if (pos>79) len -= dir;
  14  if (pos>79 && len==0) pos=0;
  15  elem.style.left = pos;
  16  elem.style.width = len;
  17  }
  18  }
  19  
  20  function remove_loading() {
  21  this.clearInterval(t_id);
  22  var targelem = document.getElementById('loader_container');
  23  targelem.style.display='none';
  24  targelem.style.visibility='hidden';
  25  var t_id = setInterval(animate,60);
  26  }


// preview_templates.js
   1  
   2  var offsetfrommouse=[15,25]; //image x,y offsets from cursor position in pixels. Enter 0,0 for no offset
   3  var displayduration=0; //duration in seconds image should remain visible. 0 for always.
   4  
   5  var defaultimageheight = 40;	// maximum image size.
   6  var defaultimagewidth = 40;	// maximum image size.
   7  
   8  var timer;
   9  
  10  function gettrailobj(){
  11  if (document.getElementById)
  12  return document.getElementById("preview_div").style
  13  }
  14  
  15  function gettrailobjnostyle(){
  16  if (document.getElementById)
  17  return document.getElementById("preview_div")
  18  }
  19  
  20  
  21  function truebody(){
  22  return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
  23  }
  24  
  25  
  26  function hidetrail(){	
  27  	gettrailobj().display= "none";
  28  	document.onmousemove=""
  29  	gettrailobj().left="-500px"
  30  	clearTimeout(timer);
  31  }
  32  
  33  function showtrail(imagename,title,width,height){
  34  	i = imagename
  35  	t = title
  36  	w = width
  37  	h = height
  38  	timer = setTimeout("show('"+i+"',t,w,h);",200);
  39  }
  40  function show(imagename,title,width,height){
  41   
  42      var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth - offsetfrommouse[0]
  43  	var docheight=document.all? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(window.innerHeight)
  44  
  45  	if( (navigator.userAgent.indexOf("Konqueror")==-1  || navigator.userAgent.indexOf("Firefox")!=-1 || (navigator.userAgent.indexOf("Opera")==-1 && navigator.appVersion.indexOf("MSIE")!=-1)) && (docwidth>650 && docheight>500)) {
  46  		( width == 0 ) ? width = defaultimagewidth: '';
  47  		( height == 0 ) ? height = defaultimageheight: '';
  48  			
  49  		width+=30
  50  		height+=55
  51  		defaultimageheight = height
  52  		defaultimagewidth = width
  53  	
  54  		document.onmousemove=followmouse; 
  55  
  56  		
  57  		newHTML = '<div class="border_preview" style="width:'+  width +'px;height:'+ height +'px"><div id="loader_container"><div id="loader"><div align="center">Loading template preview...</div><div id="loader_bg"><div id="progress"> </div></div></div></div>';
  58  		newHTML = newHTML + '<h2 class="title_h2">' + ' '+title + '</h2>'
  59  		
  60      	newHTML = newHTML + '<div class="preview_temp_load"><img onload="javascript:remove_loading();" src="' + imagename + '" border="0"></div>';
  61  		newHTML = newHTML + '</div>'; 
  62  		
  63  		if(navigator.userAgent.indexOf("MSIE")!=-1 && navigator.userAgent.indexOf("Opera")==-1 ){
  64  			newHTML = newHTML+'<iframe src="about:blank" scrolling="no" frameborder="0" width="'+width+'" height="'+height+'"></iframe>';
  65  		}		
  66  
  67  		gettrailobjnostyle().innerHTML = newHTML;
  68  		gettrailobj().display="block";
  69  	}
  70  }
  71  
  72  function followmouse(e){
  73  
  74  	var xcoord=offsetfrommouse[0]
  75  	var ycoord=offsetfrommouse[1]
  76  
  77  	var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth-15
  78  	var docheight=document.all? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(window.innerHeight)
  79  
  80  	if (typeof e != "undefined"){
  81  		if (docwidth - e.pageX < defaultimagewidth + 2*offsetfrommouse[0]){
  82  			xcoord = e.pageX - xcoord - defaultimagewidth; // Move to the left side of the cursor
  83  		} else {
  84  			xcoord += e.pageX;
  85  		}
  86  		if (docheight - e.pageY < defaultimageheight + 2*offsetfrommouse[1]){
  87  			ycoord += e.pageY - Math.max(0,(2*offsetfrommouse[1] + defaultimageheight + e.pageY - docheight - truebody().scrollTop));
  88  		} else {
  89  			ycoord += e.pageY;
  90  		}
  91  
  92  	} else if (typeof window.event != "undefined"){
  93  		if (docwidth - event.clientX < defaultimagewidth + 2*offsetfrommouse[0]){
  94  			xcoord = event.clientX + truebody().scrollLeft - xcoord - defaultimagewidth; // Move to the left side of the cursor
  95  		} else {
  96  			xcoord += truebody().scrollLeft+event.clientX
  97  		}
  98  		if (docheight - event.clientY < (defaultimageheight + 2*offsetfrommouse[1])){
  99  			ycoord += event.clientY + truebody().scrollTop - Math.max(0,(2*offsetfrommouse[1] + defaultimageheight + event.clientY - docheight));
 100  		} else {
 101  			ycoord += truebody().scrollTop + event.clientY;
 102  		}
 103  	}
 104  	gettrailobj().left=xcoord+"px"
 105  	gettrailobj().top=ycoord+"px"
 106  
 107  }


   1  
   2  <html>
   3  	<head>
   4  		<script src="preview_templates.js" language="JavaScript" type="text/javascript"></script>
   5  		<script src="loader.js" language="JavaScript" type="text/javascript"></script>
   6  	</head>
   7  
   8  	<body>
   9  		<img src="uno.jpg" border=0  border=1 style="border-color: 777777" onmouseover="showtrail('due.jpg ','Template 12306',430,449);"   onmouseout="hidetrail();">
  10  		<div style="display: none; position: absolute;z-index:110; " id="preview_div"></div>
  11  	</body>
  12  </html>

Javascript Image PreLoad

// Javascript Image Preload
   1  
   2  <script type="text/javascript">
   3      <!--
   4  
   5      if (document.images)
   6      {
   7        preload_image_object = new Image();
   8        // set image url
   9        image_url = new Array();
  10        image_url[0] = "http://mydomain.com/image0.gif";
  11        image_url[1] = "http://mydomain.com/image1.gif";
  12        image_url[2] = "http://mydomain.com/image2.gif";
  13        image_url[3] = "http://mydomain.com/image3.gif";
  14  
  15         var i = 0;
  16         for(i=0; i<=3; i++) 
  17           preload_image_object.src = image_url[i];
  18      }
  19  
  20      //-->
  21      </script> 

PNG-24 Alpha support for IE

What so much webdesigners dream about!
Get alpha channel on web, that's possible with PNG-24 images and this trick.
BE CAREFUL: that seem to work only for 10 images per page.

   1  
   2    # Display PNG-24 images with alpha channel on IE
   3    # BE CAREFUL: with this trick, only 10 PNGs seems to be supported by IE
   4    # Don't forget to set the size of your div
   5    def transpng(id, png)
   6      '<style type="text/css">
   7        <!--
   8          ' + id + ' {
   9            background-image: url(' + png + ');
  10          }
  11        -->
  12      </style>
  13      <!--[if IE]>
  14      <style>
  15        ' + id + ' {
  16          background-image: none;
  17          filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=' + png + ', sizingMethod=\'scale\');
  18        }
  19      </style>
  20      <![endif]-->'
  21    end

Resize image with .NET

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;

   1  
   2  public static byte[] ResizeImageFile(byte[] imageFile, int targetSize)
   3  {
   4      Image original = Image.FromStream(new MemoryStream(imageFile));
   5      int targetH, targetW;
   6      if (original.Height > original.Width)
   7      {
   8          targetH = targetSize;
   9          targetW = (int)(original.Width * ((float)targetSize / (float)original.Height));
  10      }
  11      else
  12      {
  13          targetW = targetSize;
  14          targetH = (int)(original.Height * ((float)targetSize / (float)original.Width));
  15      }
  16      Image imgPhoto = Image.FromStream(new MemoryStream(imageFile));
  17      // Create a new blank canvas.  The resized image will be drawn on this canvas.
  18      Bitmap bmPhoto = new Bitmap(targetW, targetH, PixelFormat.Format24bppRgb);
  19      bmPhoto.SetResolution(72, 72);
  20      Graphics grPhoto = Graphics.FromImage(bmPhoto);
  21      grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
  22      grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
  23      grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality;
  24      grPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, targetW, targetH), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel);
  25      // Save out to memory and then to a file.  We dispose of all objects to make sure the files don't stay locked.
  26      MemoryStream mm = new MemoryStream();
  27      bmPhoto.Save(mm, System.Drawing.Imaging.ImageFormat.Jpeg);
  28      original.Dispose();
  29      imgPhoto.Dispose();
  30      bmPhoto.Dispose();
  31      grPhoto.Dispose();
  32      return mm.GetBuffer();
  33  }

Crop image with .NET

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;

   1  
   2  public static byte[] CropImageFile(byte[] imageFile, int targetW, int targetH, int targetX, int targetY)
   3  {
   4      Image imgPhoto = Image.FromStream(new MemoryStream(imageFile));
   5      Bitmap bmPhoto = new Bitmap(targetW, targetH, PixelFormat.Format24bppRgb);
   6      bmPhoto.SetResolution(72, 72);
   7      Graphics grPhoto = Graphics.FromImage(bmPhoto);
   8      grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
   9      grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
  10      grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality;
  11      grPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, targetW, targetH), targetX, targetY, targetW, targetH, GraphicsUnit.Pixel);
  12      // Save out to memory and then to a file.  We dispose of all objects to make sure the files don't stay locked.
  13      MemoryStream mm = new MemoryStream();
  14      bmPhoto.Save(mm, System.Drawing.Imaging.ImageFormat.Jpeg);
  15      imgPhoto.Dispose();
  16      bmPhoto.Dispose();
  17      grPhoto.Dispose();
  18      return mm.GetBuffer();
  19  }

Lightbox JS (overlay full-size-images on the current page)

// Lightbox JS is a simple, unobtrusive script used to to overlay images on the current page. It's a snap to setup and works on all modern browsers.

// http://www.huddletogether.com/projects/lightbox/

// easy to use:
   1  
   2  <a href="images/image-1.jpg" rel="lightbox" title="my caption">image #1</a>



// the whole thing edited for Flickr: http://www.lovefool.nl/2006/01/07/lightbox-fotos/

Create Image Thumbnails (Python)

   1  
   2  # experiments with the Python Image Library (PIL)
   3  
   4  # free from:  http://www.pythonware.com/products/pil/index.htm
   5  
   6  # create 128x128 (max size) thumbnails of all JPEG images in the working folder
   7  
   8  # Python23 tested    vegaseat    25feb2005
   9  
  10  
  11  import glob
  12  import Image
  13  
  14  # get all the jpg files from the current folder
  15  
  16  for infile in glob.glob("*.jpg"):
  17    im = Image.open(infile)
  18    # convert to thumbnail image
  19  
  20    im.thumbnail((128, 128), Image.ANTIALIAS)
  21    # don't save if thumbnail already exists
  22  
  23    if infile[0:2] != "T_":
  24      # prefix thumbnail file with T_
  25  
  26      im.save("T_" + infile, "JPEG")

Change OS X's default screenshot image format

At the prompt, type this:

   1  defaults write com.apple.screencapture type image_format


Replace "image_format" with a file format name, like pdf, png, tiff, etc. Then to take screenshots, Cmd+Shift+4, then drag around the area you want to capture.
« Newer Snippets
Older Snippets »
Showing 21-30 of 32 total