Thursday, June 14, 2012

Thumbnail of images.

Yank thumbnail from image EXIF.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Drawing.Imaging; namespace Thumbs { public partial class Form1 : Form { private const int THUMBNAIL_DATA = 0x501B; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { Image thm = GetThumbnail(openFileDialog1.FileName); //now write that image to file thm.Save(openFileDialog1.FileName+".jpg", ImageFormat.Jpeg); } } Image GetThumbnail(string path) { FileStream fs = File.OpenRead(path); // Last parameter tells GDI+ not the load the actual image data Image img = Image.FromStream(fs, false, false); // GDI+ throws an error if we try to read a property when the image // doesn't have that property. Check to make sure the thumbnail property // item exists. bool propertyFound = false; for (int i = 0; i < img.PropertyIdList.Length; i++) if (img.PropertyIdList[i] == THUMBNAIL_DATA) { propertyFound = true; break; } if (!propertyFound) return null; PropertyItem p = img.GetPropertyItem(THUMBNAIL_DATA); fs.Close(); img.Dispose(); // The image data is in the form of a byte array. Write all // the bytes to a stream and create a new image from that stream byte[] imageBytes = p.Value; MemoryStream stream = new MemoryStream(imageBytes.Length); stream.Write(imageBytes, 0, imageBytes.Length); return Image.FromStream(stream); } } }

Friday, October 7, 2011

Far too long

I always end up getting sidetracked with doing other things and don't update this. I guess I'll post a couple tidbits about Android/Java stuff.
Ok, so you're writing an app and part of the requirements is removing pictures from the device. That's easy right, Get the uri of the picture from the media store, get the real path from the uri, delete the file. Viola, the picture's gone. But wait, the actual file is gone but there is still an entry for it in the media store. Simple. Give the uri to the content resolver and delete it. The entry is now gone. However, there will still be a thumbnail/entry showing in the gallery/picture app if it is running at the time of removal.

This is the code that does it:
getContentResolver().delete(uri, null, null);

Now suppose you want to show an image to the user and want to float a label or a button or a whatever over the top of the image. Here's how you do it. You put a frame layout in your layout, put the image in the frame layout, put a table layout in the frame layout, put your label/button/whatever in the table layout. (you can nest all kinds of layouts in the table to get things where you want them. An example in xml: {puts an image view on the screen and floats option buttons and labels over the top of the image on the left hand side):

<?xml version="1.0" encoding="utf-8"?>
  android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal">
    <FrameLayout android:id="@+id/frameLayout1" android:layout_height="wrap_content" android:layout_width="wrap_content">
        <ImageView android:src="@drawable/icon" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/imageView1"></ImageView>
        <TableLayout android:id="@+id/tableLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent">
            <LinearLayout android:id="@+id/linearLayout1" android:layout_width="wrap_content" android:layout_height="wrap_content">
                <Button android:text="Delete" android:layout_width="wrap_content" android:id="@+id/Discardbutton" android:layout_height="wrap_content"></Button>
            <LinearLayout android:id="@+id/linearLayout2" android:layout_width="wrap_content" android:layout_height="wrap_content">
                <Button android:text="Save" android:layout_width="wrap_content" android:id="@+id/Savebutton" android:layout_height="wrap_content"></Button>
            <LinearLayout android:id="@+id/linearLayout3" android:layout_width="wrap_content" android:layout_height="wrap_content">
                <Button android:text="Upload" android:layout_width="wrap_content" android:id="@+id/Uploadbutton" android:layout_height="wrap_content"></Button>
            <LinearLayout android:id="@+id/linearLayout4" android:layout_width="wrap_content" android:layout_height="wrap_content">
                <TextView android:id="@+id/textView1" android:textColor="#ffffffff" android:layout_width="wrap_content" android:background="#AA000000" android:layout_height="wrap_content" android:text="Filename:  "></TextView>
            <LinearLayout android:id="@+id/linearLayout5" android:layout_width="wrap_content" android:layout_height="wrap_content">
                <Button android:text="Rename" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/Renamebutton"></Button>
            <LinearLayout android:id="@+id/linearLayout6" android:layout_width="wrap_content" android:layout_height="wrap_content">
                <CheckBox android:layout_width="wrap_content" android:id="@+id/checkBox1"  android:background="#AA000000" android:layout_height="wrap_content" android:text="          "></CheckBox>
                <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Delete after upload" android:textColor="#ffffffff" android:background="#AA000000" ></TextView>

Tuesday, July 12, 2011

Moving data between forms with properties

Back when I started writing programs in C# I had no idea how to pass information between different forms. So like so many others I started by searching the internet. I found a ton of sites that showed how to do it. A lot of example code. But little explanation about what is actually going on. I think it’s more effective to know what’s happening rather than just “to do it use this code”. There are a half dozen ways you can pass data around in winforms applications in c# but the way I prefer to pass information back and forth in C# is with properties. To create a property in C# you create a field variable and a property.

Think of it like you think of a textbox. If you have textbox1 on your form you can access the .Text property of the textbox by using textbox1.text=”this”; or string variable = textbox1.text; What you’re doing is creating a form property that you can access from other forms.
Set up a windows form application in Visual Studio. Add a second form to it. In the second form create a field variable called _name and define the Name property. This is the code that does it:
private string _name;
public MyForm()
public string Name
     get { return _name; }
     set { _name = value; }
The _name variable is private which means it is visible only to the second form. When you want to change the variable in the second form you modify _name. The Name property is public and is visible everywhere in the program. When you want to change the variable anywhere except in the second form, modify Name.

Basically what happens is whenever Name is changed Set {_name = value;} takes whatever is in Name and puts it in _name. And when Name is queried, get {return _name} takes what is in _name and puts it in Name. So if we had Form1 and Form2 and our property is in Form2. Then in Form1, to send information to Form2 we would do the following:
Form2 child = new Form2();
child.Name = "Joe";
This creates an instance of Form2 called child. Then it sets the Name property to “Joe”. Then it shows the window.
If you wanted to change the name and send the new name to the form that called it, you would change the field variable in Form2 and then get the Name property in Form1.
So in Form2 you would have:
_name = “New Name”;
And in Form1 (after the ShowDialog()) you would read the property
string newName = child.Name;

See. Clear as mud.

Monday, July 11, 2011

Greetings Earthling.

tl;dr version: I'm a geek. I'm going to blog about programming. Some personal history/get to know you stuff below.

I'll be using this to share tidbits of useful coding information for various platforms. I've been working with and learning computers and technology since 1984 and thought I would share some of the things that I've learned over the years/found useful. But before I get into all of that techie stuff, a little about me. I'm the typical geek. D&D, video games, gadgets and technology. I'm also the typical country boy. I know how to pluck a chicken, roof a house, change an engine, plumb a bathroom, and skin a rabbit (among other things). 

I've made my living through the years doing Systems Administration and support (usually the only person in the IT department) and love my job.  The first computer I owned was a Tandy Color Computer 2 back when I was in high school. Like all the other kids I thought the games were cool (Oregon Trail, Robot Odyssey, Zork, etc) but it really wasn't enough for me.

I wanted to know how thy did that. How do you get a computer to do that? So I dove in head first. A year later I was submitting my first program for publication. A text adventure game written in BASIC called Save the Earth. It was published through T&D Software. A monthly publication where they mailed out disks and cassettes to their subscribers with various programs on them (games, utilities, productivity, etc). Shortly after the BASIC program I decided that it still wasn't enough. I needed to figure out how things worked on a more basic level so I dove into ASM for the 6809 processor. Once I wrapped my head around that I submitted my second program for publication. A utility in ASM that let you change the background color and font color on the Color Computer that was persistent until a system restart. Simple stuff but it gave me a good foundation to understand what was really going on inside computers.

From there I went on to helping out the school district with their student attendance/records software. Worked part time helping the one computer business doing things like building systems and running network cabling. Started college and worked in the computer lab under their college work study program. I also ventured into the 'dark side' of computers for a bit. BBS's, hacking, cracking, piracy, all of that sordid stuff. I think I learned more in that couple years than I did in any other time period. Then I was done with college and everything changed. I had to get a real job. I ended up going to work for an insuarnce agency as their Jr. Network Administrator. Which pretty much meant I did all of the physical work like building and moving systems, running cable, answering help desk calls, etc.

This went on a couple of years, the admin left for greener pastures, I got a girlfriend, the company was sold, changed names, I lost a girlfriend, new department head, new girlfriend, got married, had a severe personality conflict with the department head, and it was over. Moved to Dallas, got a new job as Network Administrator (same job, new title, better pay, more people, longer hours). Spent 4 years without sleep keeping everything running, went on vacation and came back to the locks on the doors changed. And that was over. Went to work a week later where I am now. I love this job. I love the people I work for and with. The job is still the same job as before but with much much more. Everything from running network cable to writing industry specific applications. I've been here for 11 years and plan on being here for the duration.

There's so much more I could post but I'd bore you.