Archive of posts filed under the Images category.

Showing images from database in in Silverlight

You can show images in Silverlight using Image control.  You can furthermore bind Source property of the image to byte[] type property of a business object.  You would think this is sufficient, but it is not.  You will need a converter to convert byte array into image source.

Here is the class for converter

public class ImageSourceConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        BitmapImage bitmap = new BitmapImage();
        bitmap.SetSource(new MemoryStream((Byte[])value));
        return bitmap;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Here is how this class is used in a control:

<UserControl.Resources>
       <helpers:ImageConverter x:Key="ConvertImage"/>
   </UserControl.Resources>

<Image Source="{Binding Path=MyByteArrayPropertyName, Converter={StaticResource ConvertImage}}" />

How to draw bitmaps in code

So, I had a task to create a graph in code

Not an easy task at you can see.  It took me about 12 hours to get it done from scratch.  My class takes the following parameters: 4 values – one for each color and bitmap size (width and height).

Here are some code samples:

Create a bitmap

Bitmap returnValue = new Bitmap(width, height);

Draw axes:

private static void DrawAxes(Bitmap result)
    {
      Pen axesPen = new Pen(Color.Black, 1);
      Graphics worker = Graphics.FromImage(result);
      worker.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
      // draw diagonal axis lines
      worker.DrawLine(axesPen, GetTopLeftCorner(result), GetBottomRightCorner(result));

To draw red color polygon:

PointF[] coordinates = new PointF[] {
          GetRedCorner(result, red),
          redYellowIntersect,
          middle,
          greenRedIntersect};
worker.DrawLine(graphPen, redYellowIntersect, GetRedCorner(result, red));
worker.DrawLine(graphPen, greenRedIntersect, GetRedCorner(result, red));
worker.FillPolygon(redBrush, coordinates);

One fun part was to write a code to determine where two lines intersect.  Here is entire function:

/// <summary>
    /// Find a point at which two lines intersect
    /// </summary>
    /// <param name="line1">First line</param>
    /// <param name="line2">Second line</param>
    /// <param name="middle">Coordinates of the middle of the bitmap</param>
    /// <param name="intersectionType">Does this intersect cross x or y axis</param>
    /// <returns></returns>
    private static PointF LinesIntersect(Line line1, Line line2, PointF middle, IntersectType intersectionType)
    {
      PointF returnValue = new PointF();
      float a1, a2, b1, b2;
      if (line1.End.X == line1.Start.X)
        b1 = (line1.End.Y – line1.Start.Y);
      else
        b1 = (line1.End.Y – line1.Start.Y) / (line1.End.X – line1.Start.X);

      if (line2.End.X == line2.Start.X)
        b2 = (line2.End.Y – line2.Start.Y);
      else
        b2 = (line2.End.Y – line2.Start.Y) / (line2.End.X – line2.Start.X);

      a1 = line1.Start.Y – b1 * line1.Start.X;
      a2 = line2.Start.Y – b2 * line2.Start.X;
      returnValue.X = -(a1 – a2) / (b1 – b2);
      returnValue.Y = a1 + b1 * returnValue.X;

      switch (intersectionType)
      {
        case IntersectType.Horizontal:
          returnValue.Y = middle.Y;
          break;
        case IntersectType.Vertical:
          returnValue.X = middle.X;
          break;
        default:
          throw new Exception("Invalid type");

      }
      return returnValue;
    }

Here are internal classes used in it:

/// <summary>
    /// Definition for a line - start and end points
    /// </summary>
    private struct Line
    {
      public PointF Start;
      public PointF End;

      public Line(PointF start, PointF end)
      {
        Start = start;
        End = end;
      }
    }

    /// <summary>
    /// Type of intersection - does cross y or x axis
    /// </summary>
    private enum IntersectType
    {
      Horizontal,
      Vertical
    }