[英]Create Image with repeating Existing Image
我有一個像素大小為1024(寬度)x 1024(高度)像素的圖像。 假設用戶想將該圖像重復2次以創建另一個圖像。 因此,現在的像素為2048 x2048。我可以通過代碼獲取像素,但圖像顯示不完整。
如何使用WPF和C#做到這一點?
以上情況更多:1)用戶只想重復高度,而不是寬度,則圖像將是1024 x 2048 2)用戶只想重復寬度3次,則圖像將是3072 x 1024
做您想要的事情的直接方法是創建一個具有所需大小的占位符圖像:例如,如果您有一個圖像(寬度,高度),則可以創建(n *寬度,m *高度),然后復制像素。
如果需要,請告訴我,我會為您提供一些代碼。
private static Bitmap ResizeBitmap(Bitmap sourceBMP, Int32 widthMultiplier,
Int32 heightMultiplier)
{
var newWidth = sourceBMP.Width * widthMultiplier;
var newHeight = sourceBMP.Height * heightMultiplier;
var result = new Bitmap(newWidth, newHeight);
using (Graphics g = Graphics.FromImage(result))
g.DrawImage(sourceBMP, 0, 0, newWidth, newHeight);
return result;
}
static void Main(string[] args)
{
var widthM = 2;
var heightM = 2;
var image = (Bitmap)Image.FromFile(@"E:\YOUR_IMAGE_HERE.png", true);
var newImage = ResizeBitmap(image, widthM, heightM);
for(var i=0; i<image.Width;++i)
for(var j=0; j<image.Height;++j)
{
var pixelToCopy = image.GetPixel(i, j);
for (var k = 0; k < widthM; ++k)
for (var l = 0; l < heightM; ++l)
newImage.SetPixel(k * image.Width + i,
l * image.Height + j,
pixelToCopy);
}
newImage.Save(@"E:\NEW_BIG_IMAGE_HERE.png", ImageFormat.Png);
}
}
順便說一句,GetPixel和SetPixel速度很慢。 因此,您可能會采用一些不安全的代碼並用它重寫循環。 在MSDN上查看示例
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.