Thursday, December 9, 2010

Creating and merging two Images in C#

//Creating new Image
var bitmap1 = new Bitmap(1, 1);
var font = new Font("Arial", 25);
Graphics graphics = Graphics.FromImage(bitmap1);
int width1 = (int)graphics.MeasureString("hello world this is custome image", font).Width;
int height1 = (int)graphics.MeasureString("hello world this is custome image", font).Height;
bitmap1 = new Bitmap(bitmap1, new Size(width1, height1));
graphics = Graphics.FromImage(bitmap1);
graphics.Clear(Color.Gray);
graphics.DrawString("hello world this is custome image", font, new SolidBrush(Color.DarkGray), 0, 0);
graphics.Flush();

//accessing existing Image
var fileStream = new FileStream(Server.MapPath("//images//sky.jpg"), FileMode.Open);
var bytes1 = new byte[fileStream.Length];
fileStream.Read(bytes1, 0, bytes1.Length);
fileStream.Close();
var bitmap3 = new Bitmap(new MemoryStream(bytes1));

int width = bitmap3.Width;
int height = bitmap3.Height + height1;
var finalImage = new Bitmap(width, height);

//merging two images
//get a graphics object from the image so we can draw on it

var images = new List{bitmap3,bitmap1};
using (graphics = Graphics.FromImage(finalImage))
{
//set background color
graphics.Clear(Color.Transparent);

//go through each image and draw it on the final image
int offset = 0;
foreach (Bitmap image in images)
{
graphics.DrawImage(image,
new Rectangle(0, offset, image.Width, image.Height));
offset += image.Height;
}
}

// save on memory
var memoryStream = new MemoryStream();
finalImage.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] bytes = memoryStream.ToArray();

// throwing on web
HttpContext.Current.Response.ContentType = "image/jpeg";
HttpContext.Current.Response.Expires = 0;
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.BinaryWrite(bytes);
HttpContext.Current.Response.End();

Wednesday, December 8, 2010

Rotate any control in silverlight

Rotate any control in silverlight

var messageTextBlock = new TextBlock
{
Text = "Messages",
Margin = new Thickness(5,20,0,0),
//FontWeight = FontWeights.Bold,
Foreground = new SolidColorBrush(Colors.White),
VerticalAlignment = VerticalAlignment.Top,
Width = 70,
Height=20,
TextWrapping = TextWrapping.Wrap,
};
RotateTransform r = new RotateTransform();
messageTextBlock.RenderTransform = r;
r.CenterX = 0;
r.CenterY = 0;
r.Angle = 90;

Tuesday, August 10, 2010

FluidMoveBehavior


Learn to create applications that change layout and visual appearance using smooth, dynamic and visually rich transitions without writing code. Come see new features in Expression Blend that raise the bar, making it even easier to create amazing applications that will delight users

Tuesday, May 18, 2010

custom drag drop in silverlight

custom drag drop in silverlight
#region custom Drag Drop

double _horizDrag = 0;
double _vertDrag = 0;
double _mouseVerticalPosition;
double _mouseHorizontalPosition;
bool _captured = false;
bool _isDragStart = false;
private void EndDragPopupMouseLeftUp()
{

_horizDrag = _vertDrag = 0;
_captured = false;
popupContent.Content = null;
popupContent.ContentTemplate = null;
//popup1.ReleaseMouseCapture();
_mouseVerticalPosition = -1;
_mouseHorizontalPosition = -1;
var ts = new ThreadStart(DragHold);
// create new thread
var thrd = new Thread(ts);
// start thread
thrd.Start();
}
public void DragHold()
{
// makes the main thread sleep - let sub thread to run
Thread.Sleep(100);
//if (!IsOver)
_isDragStart = false;
}
private void MoveDragPopupMouseMove(MouseEventArgs e)
{
if (_captured)
{

// Calculate the current position of the object.
double deltaV = e.GetPosition(null).Y - _mouseVerticalPosition;
double deltaH = e.GetPosition(null).X - _mouseHorizontalPosition;
_horizDrag += deltaH;
_vertDrag += deltaV;
double newTop = deltaV + (double)popup1.VerticalOffset;
double newLeft = deltaH + (double)popup1.HorizontalOffset;

// Set new position of object.
popup1.VerticalOffset = newTop;
popup1.HorizontalOffset = newLeft;


// Update position global variables.
_mouseVerticalPosition = e.GetPosition(null).Y;
_mouseHorizontalPosition = e.GetPosition(null).X;
var temBor = popupContent.Content as Border;
var lbl = temBor.Child as Label;
lbl.Content = "Selected to Drop (X=" + _mouseVerticalPosition + ",Y=" + _mouseHorizontalPosition + ")";
}
}
private void StartDragMouseLeftDown(MouseEventArgs e,object tag)
{
Thread.Sleep(350);

var myEffect = new DropShadowEffect
{
Color = Colors.Black,
BlurRadius = 5,
Direction = 280,
Opacity = 0.7,
ShadowDepth = 5,
};

var borde = new Border
{
BorderThickness = new Thickness(2),
BorderBrush = new SolidColorBrush(Colors.White),
Effect = myEffect,
Opacity = .7,
Tag = tag,
};
var lbl = new Label
{
Content = "Selected to Drop",
Foreground = new SolidColorBrush(Colors.Blue),
Background = new SolidColorBrush(Colors.DarkGray),
//BorderBrush = new SolidColorBrush(Colors.Orange),
BorderThickness = new Thickness(0)
};

borde.Child = lbl;
popupContent.Content = borde;
//popup1.CaptureMouse();
_captured = true;
_mouseVerticalPosition = e.GetPosition(null).Y;
_mouseHorizontalPosition = e.GetPosition(null).X;
popup1.HorizontalOffset = _mouseHorizontalPosition;
popup1.VerticalOffset = _mouseVerticalPosition;
_isDragStart = true;
}
private void GetDragDropElement()
{
if (_isDragStart)
{
MessageBox.Show("dropd on me");
_isDragStart = false;
}

}
//on .xaml file
/*
*/
#endregion

Tuesday, April 20, 2010

Get parent type Object ex. in treeview

void method
{
// calling the method
TreeViewItem trvItem = GetParentTreeViewItem((DependencyObject)sender);
}
// this will return the desire type exg. here it is returning TreeViewItem type..
private static TreeViewItem GetParentTreeViewItem(DependencyObject item)
{

if (item != null)
{
DependencyObject parent = VisualTreeHelper.GetParent(item);
TreeViewItem parentTreeViewItem = parent as TreeViewItem;
return (parentTreeViewItem != null) ? parentTreeViewItem : GetParentTreeViewItem(parent);
}
return null;

}

Monday, April 12, 2010

Get xml from web services and use in tree view of silver light

private void cmbTaxonomy_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
_objWebService.getTreeOfClassCompleted += new EventHandler(_objWebService_getTreeOfClassCompleted);
_objWebService.getTreeOfClassAsync(cmbTaxonomy.SelectedItem.ToString());
}
const string xmlNodeTextAttName = "Name";
const string xmlNodeTagAttId = "Id";
const string xmlNodeTagClass = "Class";
const string xmlNodeTagRoot = "Root";
void _objWebService_getTreeOfClassCompleted(object sender, getTreeOfClassCompletedEventArgs e)
{
XDocument xDocument = XDocument.Parse(e.Result);
trvHierarchy.Items.Clear();
if (xDocument.Root != null)
if(xDocument.Root.Elements().ToArray().Count()>0)
{
foreach (var VARIABLE in xDocument.Root.Elements().ToArray())
{
if (VARIABLE != null)
{
TreeViewItem parentNodeItem = new TreeViewItem
{
IsExpanded = true,
Header = CreateTreeViewItemHeader(VARIABLE.Attribute(xmlNodeTextAttName).Value, Convert.ToInt64(VARIABLE.Attribute(xmlNodeTagAttId).Value))
};
AddChildItemsInTree(VARIABLE, parentNodeItem);
trvHierarchy.Items.Add(parentNodeItem);
}

}
}
}

private void AddChildItemsInTree(XElement ParentVariable, TreeViewItem parentNodeItem)
{
if(ParentVariable.HasElements)
{
foreach(var variableChild in ParentVariable.Elements().ToArray())
{
if (ParentVariable != null)
{
TreeViewItem childNodeItem = new TreeViewItem
{
IsExpanded = true,
Header = CreateTreeViewItemHeader(variableChild.Attribute(xmlNodeTextAttName).Value, Convert.ToInt64(variableChild.Attribute(xmlNodeTagAttId).Value))
};
//childNodeItem.Header = variableChild.Attribute(xmlNodeTextAttName).Value;
AddChildItemsInTree(variableChild, childNodeItem);
parentNodeItem.Items.Add(childNodeItem);
}
}
}
}

Creat xml for web services and return it

public string getTreeOfClass(string taxonomyName)
{
//get ClassElementsName
var taxsonomyId = _exhibitDb.Taxonomies.SingleOrDefault(o => o.Taxonomy1 == taxonomyName);

var classeList = from c in _exhibitDb.Classes join tc in _exhibitDb.TaxonomyClasses on c.ID equals tc.ClassID where (tc.TaxonomyID.Equals(taxsonomyId.ID)) select c;
//XDocument document = new XDocument();
//document.Root
const string xmlNodeTextAttName = "Name";
const string xmlNodeTagAttId = "Id";
const string xmlNodeTagClass = "Class";
const string xmlNodeTagRoot = "Root";
XmlWriterSettings settings = new XmlWriterSettings
{
Indent = true,
OmitXmlDeclaration = true,
NewLineOnAttributes = false,
Encoding = Encoding.UTF8
};
StringBuilder sb = new StringBuilder();
XmlWriter writer = XmlWriter.Create(sb, settings);
//writing xml

if (writer != null)
{
writer.WriteStartDocument();
writer.WriteStartElement(xmlNodeTagRoot);
foreach (var v in classeList)
{
writer.WriteStartElement(xmlNodeTagClass);
writer.WriteAttributeString(xmlNodeTextAttName, v.Class1);
writer.WriteAttributeString(xmlNodeTagAttId, v.ID.ToString());
AddChildElement(writer, v.ID, xmlNodeTagAttId, xmlNodeTextAttName, xmlNodeTagClass);
writer.WriteEndElement();
}

writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
}
return sb.ToString();// xmlDocument;
}