代码改变世界

WP7备注(35)(Vector Graphics|Raster Graphics)

2011-05-09 09:40  血糯米Otomii  阅读(387)  评论(0编辑  收藏  举报

Vector Graphics Pass

--------------------------------------------------------------------------------------------------

Raster Graphics:

image

BitmapSource拥有2个字段,1个方法:

PixelWidth,PixelHeight
SetSource(Stream stream)

BitmapImage拥有2个字段,3个事件来记录图片加载过程:

UriSource,CreateOptions

WriteableBitmap:

WriteableBitmap writeableBitmap = new WriteableBitmap(element, transform);
transform可以为null

writeableBitmap.Render(element, transform);

writeableBitmap.Invalidate();重新绘制Bitmap

PhotoChooserTask photoChooser = new PhotoChooserTask();
public MainPage()
{
InitializeComponent();
photoChooser.Completed += OnPhotoChooserCompleted;
}
protected override void OnManipulationStarted(ManipulationStartedEventArgs args)
{
int dimension = (int)Math.Min(ContentPanel.ActualWidth,
ContentPanel.ActualHeight) - 8;
photoChooser.PixelHeight = dimension;
photoChooser.PixelWidth = dimension;
photoChooser.Show();
args.Complete();
args.Handled = true;
base.OnManipulationStarted(args);
}
void OnPhotoChooserCompleted(object sender, PhotoResult args)
{
if (args.Error != null || args.ChosenPhoto == null)
return;
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(args.ChosenPhoto);
Image imgBase = new Image();
imgBase.Source = bitmapImage;
imgBase.Stretch = Stretch.None;
// Upper-left
WriteableBitmap writeableBitmap = new WriteableBitmap(bitmapImage.PixelWidth /
2,
bitmapImage.PixelHeight /
2);
writeableBitmap.Render(imgBase, null);
writeableBitmap.Invalidate();
imgUL.Source = writeableBitmap;
// Upper-right
writeableBitmap = new WriteableBitmap(bitmapImage.PixelWidth / 2,
bitmapImage.PixelHeight / 2);
TranslateTransform translate = new TranslateTransform();
translate.X = -bitmapImage.PixelWidth / 2;
writeableBitmap.Render(imgBase, translate);
writeableBitmap.Invalidate();
imgUR.Source = writeableBitmap;
// Lower-left
writeableBitmap = new WriteableBitmap(bitmapImage.PixelWidth / 2,
bitmapImage.PixelHeight / 2);
translate.X = 0;
translate.Y = -bitmapImage.PixelHeight / 2;
writeableBitmap.Render(imgBase, translate);
writeableBitmap.Invalidate();
imgLL.Source = writeableBitmap;
// Lower-right
writeableBitmap = new WriteableBitmap(bitmapImage.PixelWidth / 2,
bitmapImage.PixelHeight / 2);
translate.X = -bitmapImage.PixelWidth / 2;
writeableBitmap.Render(imgBase, translate);
writeableBitmap.Invalidate();
imgLR.Source = writeableBitmap;
txtblk.Visibility = Visibility.Collapsed;
}

CircularGradient:

const int RADIUS = 200;
public MainPage()
{
InitializeComponent();
WriteableBitmap writeableBitmap = new WriteableBitmap(2 * RADIUS, 2 *
RADIUS);
for (int y = 0; y < writeableBitmap.PixelWidth; y++)
for (int x = 0; x < writeableBitmap.PixelHeight; x++)
{
if (Math.Sqrt(Math.Pow(x - RADIUS, 2) + Math.Pow(y - RADIUS, 2)) <
RADIUS)
{
double angle = Math.Atan2(y - RADIUS, x - RADIUS);
byte R = (byte)(255 * Math.Abs(angle) / Math.PI);
byte B = (byte)(255 - R);
int color = 255 << 24 | R << 16 | B;
writeableBitmap.Pixels[y * writeableBitmap.PixelWidth + x] =
color;
}
}
writeableBitmap.Invalidate();
img.Source = writeableBitmap;
}

image

----------------------------------------------

拼图游戏:略

颜色:略