问题描述
我是WPF的新学员。我有一个问题。
我有一张图片,宽度:360,高度:360。在这里,我想裁剪如下图像:
(0,0)到(120,120)保存到第一个ImageSource对象,
(120,0)到(240,120)保存到第二个ImageSource对象,
(240,0)到(360,120)保存到第三个ImageSource对象;,
......
请参阅下图中的更多详情:
我的代码示例如下:
private void CutImage(string img)
{
int iLeft = 0;
int iTop = 0;
int count = 0;
Image thisImg = new Image();
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri(img,UriKind.Relative);
src.CacheOption = BitmapCacheOption.OnLoad;
src.EndInit();
thisImg.Source = src;
for(int i = 0; i< 3; i ++)
{
iTop = i * 120;
for(int j = 0; j< 3; j ++)
{
iLeft = j * 120;
Canvas canvas = new Canvas();
Rectangle destRect = new Rectangle();
destRect.SetValue(Canvas.LeftProperty,(double)0);
destRect.SetValue(Canvas.TopProperty,(double)0);
destRect.Width = destRect.Height = 120;
Rect srcRect = new Rect();
srcRect.X = iLeft;
srcRect.Y = iTop;
srcRect.Width = srcRect.Height = 120;
thisImg.Clip = new RectangleGeometry(srcRect);
thisImg.Clip.SetValue(Canvas.TopProperty,(double)iTop);
thisImg.Clip.SetValue(Canvas.LeftProperty,(double)iLeft);
thisImg.Clip.SetValue(Canvas.WidthProperty,(double)120);
thisImg.Clip.SetValue(Canvas.HeightProperty,(double)120);
objImg [count ++] =(ImageSource)thisImg.GetValue(Image.SourceProperty);
}
}
}
但它不起作用正如我所料,似乎所有的ImageSource对象都存储相同的图像,而不是corping部分。任何人都可以帮助我吗?使用执行此操作:
private void CutImage(string img)
{
int count = 0;
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri(img,UriKind.Relative);
src.CacheOption = BitmapCacheOption.OnLoad;
src.EndInit();
for(int i = 0; i< 3; i ++)
for(int j = 0; j< 3; j ++)
objImg [count ++] = new CroppedBitmap(src,new Int32Rect(j * 120,i * 120,120,120));
}
I am new learner to WPF. here I got a question.I have a image, width:360, height:360. Here I want to crop this image like below:
( 0,0 ) to (120,120) save to the first ImageSource object,
(120,0 ) to (240,120) save to the second ImageSource object,
(240,0 ) to (360,120) save to the third ImageSource object;,
……
pls see more details in below picture:
My code sample below:
private void CutImage(string img)
{
int iLeft = 0;
int iTop = 0;
int count = 0;
Image thisImg = new Image();
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri(img, UriKind.Relative);
src.CacheOption = BitmapCacheOption.OnLoad;
src.EndInit();
thisImg.Source = src;
for (int i = 0; i < 3; i++)
{
iTop = i * 120;
for (int j = 0; j < 3; j++)
{
iLeft = j * 120;
Canvas canvas = new Canvas();
Rectangle destRect = new Rectangle();
destRect.SetValue(Canvas.LeftProperty, (double)0);
destRect.SetValue(Canvas.TopProperty,(double)0);
destRect.Width = destRect.Height = 120;
Rect srcRect = new Rect();
srcRect.X = iLeft;
srcRect.Y = iTop;
srcRect.Width = srcRect.Height = 120;
thisImg.Clip = new RectangleGeometry(srcRect);
thisImg.Clip.SetValue(Canvas.TopProperty, (double)iTop);
thisImg.Clip.SetValue(Canvas.LeftProperty, (double)iLeft);
thisImg.Clip.SetValue(Canvas.WidthProperty, (double)120);
thisImg.Clip.SetValue(Canvas.HeightProperty,(double)120);
objImg[count++] = (ImageSource)thisImg.GetValue(Image.SourceProperty);
}
}
}
but it doesn't work as I expected, seems that all the ImageSource objects store the same image, not the corping part. Any one can help me ? thx.
Use CroppedBitmap to do this:
private void CutImage(string img)
{
int count = 0;
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri(img, UriKind.Relative);
src.CacheOption = BitmapCacheOption.OnLoad;
src.EndInit();
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
objImg[count++] = new CroppedBitmap(src, new Int32Rect(j * 120, i * 120, 120, 120));
}
这篇关于如何在WPF中裁剪图像并保存到ImageSource?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!