RSS

Monthly Archives: January 2015

Set transparent background of an imageview in Android

How to calculate transparent color in Android with 8 digit format (#xxxxxxxx):

Ex we want to use only 50% of background color

x = 50/100 * 255 = 127.5 => x = 127 and convert 127 from decimal to hexadecimal

=> x (hex) = 7F.

Finnaly => 50% => #7F000000

 

Referenced by: http://stackoverflow.com/questions/1492554/set-transparent-background-of-an-imageview-in-android

Advertisements
 
Leave a comment

Posted by on January 29, 2015 in Android, Mobile Development

 

Image Resizing and Cropping in C#

By: JasonJano’s Blog

So the other day I put up a c# wrapper for ffmpeg. The wrapper I built is a part of my own asset storage system which I guess I will probably be putting up in chunks over the next few weeks. Today I have decided to put up my image resizing tools for your use.

Resize Now or Resize Later?

There are a couple theories to image resizing, should the image be resized at the point it gets uploaded, or at the point it gets sent out to the client?

Each method has advantages and disadvantages. Resizing at upload is good because it speeds up time to delivery as the processor has already done the resizing, additionally, the image is only resized once, vs. if the image is resized every time it is downloaded, it could be resized countless times. The problem with resizing at upload, however, is that it is now resized — you can’t un-resize it. Resizing at download then, is more processor intensive as it is resized every time someone requests a different size (one could probably get around some of this with caching, but the no matter how you slice it, it is still more processor intensive). The nice thing is that based on querystring or, however you pass the resizer data, you get a customized image size.

I have my own theory on this. If one has a ton of storage space (which most of us do have available to us now adays) it makes sense to resize in some basic sizes at upload while keeping the original as well for future resizing, if needed. Most good graphic designers break all their pages up into grids of magic 3rds anyways and would, generally, have ideas about their perfect image sizes for thumbnails, swatches, large previews, etc..

In another post I will detail how I get all of these different sizes and what not stored for use, but — for now — I will just give you the tools with which to do resizing and cropping in one spot.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//Overload for crop that default starts top left of the image.
public static System.Drawing.Image CropImage(System.Drawing.Image Image, int Height, int Width)
{
    return CropImage(Image, Height, Width, 0,0);
}
//The crop image sub
public static System.Drawing.Image CropImage(System.Drawing.Image Image, int Height, int Width, int StartAtX, int StartAtY)
{
    Image outimage;
    MemoryStream mm = null;
    try
    {
        //check the image height against our desired image height
        if (Image.Height < Height) {
            Height = Image.Height;
        }
        
        if (Image.Width < Width) {
            Width = Image.Width;
        }
        
        //create a bitmap window for cropping
        Bitmap bmPhoto = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
        bmPhoto.SetResolution(72, 72);
        
        //create a new graphics object from our image and set properties
        Graphics grPhoto = Graphics.FromImage(bmPhoto);
        grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
        grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
        grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality;
        
        //now do the crop
        grPhoto.DrawImage(Image, new Rectangle(0, 0, Width, Height), StartAtX, StartAtY, Width, Height, GraphicsUnit.Pixel);
        
        // Save out to memory and get an image from it to send back out the method.
        mm = new MemoryStream();
        bmPhoto.Save(mm, System.Drawing.Imaging.ImageFormat.Jpeg);
        Image.Dispose();
        bmPhoto.Dispose();
        grPhoto.Dispose();
        outimage = Image.FromStream(mm);
        return outimage;
    }
    catch (Exception ex)
    {
        throw new Exception("Error cropping image, the error was: " + ex.Message);
    }
}
//Hard resize attempts to resize as close as it can to the desired size and then crops the excess
public static System.Drawing.Image HardResizeImage(int Width, int Height, System.Drawing.Image Image)
{
    int width = Image.Width;
    int height = Image.Height;
    Image resized = null;
    if (Width > Height)
    {
        resized = ResizeImage(Width, Width, Image);
    }
    else
    {
        resized = ResizeImage(Height, Height, Image);
    }
    Image output = CropImage(resized, Height, Width);
    //return the original resized image
    return output;
}
//Image resizing
public static System.Drawing.Image ResizeImage(int maxWidth, int maxHeight, System.Drawing.Image Image)
{
    int width = Image.Width;
    int height = Image.Height;
    if (width > maxWidth || height > maxHeight)
    {
        //The flips are in here to prevent any embedded image thumbnails -- usually from cameras
        //from displaying as the thumbnail image later, in other words, we want a clean
        //resize, not a grainy one.
        Image.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipX);
        Image.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipX);
        float ratio = 0;
        if (width > height)
        {
            ratio = (float)width / (float)height;
            width = maxWidth;
            height = Convert.ToInt32(Math.Round((float)width / ratio));
        }
        else
        {
            ratio = (float)height / (float)width;
            height = maxHeight;
            width = Convert.ToInt32(Math.Round((float)height / ratio));
        }
        //return the resized image
        return Image.GetThumbnailImage(width, height, null, IntPtr.Zero);
    }
    //return the original resized image
    return Image;
}

Copy from: https://jasonjano.wordpress.com/2010/02/13/image-resizing-and-cropping-in-c/

 
Leave a comment

Posted by on January 26, 2015 in C#, Window Application