C# 显示完整图像而不裁剪C部分#
我想比较两幅图像,其中只有“打印日期”不同,我只想裁剪“日期”区域。但是我想显示没有作物区域的完整图像(不仅仅是作物区域) 我用来裁剪的代码C# 显示完整图像而不裁剪C部分#,c#,image,C#,Image,我想比较两幅图像,其中只有“打印日期”不同,我只想裁剪“日期”区域。但是我想显示没有作物区域的完整图像(不仅仅是作物区域) 我用来裁剪的代码 static void Main(string[] args) { Bitmap bmp = new Bitmap(@"C:\Users\Public\Pictures\Sample Pictures\1546.jpg"); Rectangle rect = new Rectangle(0, 0, bmp.Width
static void Main(string[] args)
{
Bitmap bmp = new Bitmap(@"C:\Users\Public\Pictures\Sample Pictures\1546.jpg");
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData rawOriginal = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
int origByteCount = rawOriginal.Stride * rawOriginal.Height;
byte[] origBytes = new Byte[origByteCount];
System.Runtime.InteropServices.Marshal.Copy(rawOriginal.Scan0, origBytes, 0, origByteCount);
//I want to crop a 100x100 section starting at 15, 15.
int startX = 15;
int startY = 15;
int width = 100;
int height = 100;
int BPP = 4; //4 Bpp = 32 bits, 3 = 24, etc.
byte[] croppedBytes = new Byte[width * height * BPP];
//Iterate the selected area of the original image, and the full area of the new image
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width * BPP; j += BPP)
{
int origIndex = (startX * rawOriginal.Stride) + (i * rawOriginal.Stride) + (startY * BPP) + (j);
int croppedIndex = (i * width * BPP) + (j);
//copy data: once for each channel
for (int k = 0; k < BPP; k++)
{
croppedBytes[croppedIndex + k] = origBytes[origIndex + k];
}
}
}
//copy new data into a bitmap
Bitmap croppedBitmap = new Bitmap(width, height);
BitmapData croppedData = croppedBitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
System.Runtime.InteropServices.Marshal.Copy(croppedBytes, 0, croppedData.Scan0, croppedBytes.Length);
bmp.UnlockBits(rawOriginal);
croppedBitmap.UnlockBits(croppedData);
croppedBitmap.Save(@"C:\Users\Public\Pictures\Sample Pictures\AFTERCROP_CROP.jpg");
bmp.Save(@"C:\Users\Public\Pictures\Sample Pictures\AFTERCROP-ORIG.jpg");
}
static void Main(字符串[]args)
{
位图bmp=新位图(@“C:\Users\Public\Pictures\Sample Pictures\1546.jpg”);
矩形rect=新矩形(0,0,bmp.Width,bmp.Height);
BitmapData rawOriginal=bmp.LockBits(新矩形(0,0,bmp.Width,bmp.Height)、ImageLockMode.ReadOnly、PixelFormat.Format32bppArgb);
int origByteCount=rawOriginal.Stride*rawOriginal.Height;
byte[]origBytes=新字节[origByteCount];
System.Runtime.InteropServices.Marshal.Copy(rawOriginal.Scan0,origBytes,0,origByteCount);
//我想从15,15开始裁剪一个100x100的部分。
int startX=15;
int startY=15;
整数宽度=100;
整数高度=100;
int BPP=4;//4 BPP=32位,3=24位,以此类推。
字节[]裁剪字节=新字节[宽度*高度*BPP];
//迭代原始图像的选定区域和新图像的整个区域
对于(int i=0;i
您的代码有点过于复杂,您似乎对裁剪是什么感到困惑-裁剪意味着拍摄原始图片的一部分。相反,您似乎希望将原始图片的某些部分涂成黑色:
最简单的方法是在原始图像上绘制一个简单的填充矩形:
var bmp = Bitmap.FromFile(@"C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg");
using (var gr = Graphics.FromImage(bmp))
{
gr.FillRectangle(Brushes.Black, 50, 50, 200, 200);
}
如果您还想保留原始位图,可以将其复制过来。Graphics.DrawImage有什么问题?至于你的问题,没有任何意义。展示什么?哪里或者你的意思是你想把图片的一部分涂掉,而不是裁剪它<代码>图形。FillRectangle应该可以。是的,我想遮住裁剪部分。这不是单词
裁剪
的意思。谢谢,我昨天在代码中也做了同样的操作,我需要多次遮光。